开发者

How to stub Active Resource?

开发者 https://www.devze.com 2023-03-23 09:52 出处:网络
My Active Resource connects to some stupid external service that takes a while to respond for whatever reason. This is a little too nagging. I would like to stub Active Resource during development to

My Active Resource connects to some stupid external service that takes a while to respond for whatever reason. This is a little too nagging. I would like to stub Active Resource during development to speed up my development time.

Is this a good thing to do? I think it is. If you think otherwise, please explain.

And is there a mechanism to stub it out based on a switch in environment configuration file, possibly any gem/plugin that you have used for this purpose?

What and how do 开发者_开发知识库you do all these in your experience?


I recommend using FakeWeb. I used this on a project recently and it allowed me to register a number of external urls with a predefined response. In your test setup you could do:

FakeWeb.register_uri(:get, %r|users.xml|, :body => File.read("spec/factories/xml/users.xml"))

Now whenever active resource requests anyhost.com/users.xml (in test environment), you'll instead immediately get the contents of the file your referred to. I like this approach because when you're testing a model, you don't really want to be testing the external service too. I'd leave that level of testing to an integration test.

This won't affect development or production environments, so you can use your stupid external service as usual.

0

精彩评论

暂无评论...
验证码 换一张
取 消