开发者

How should I deal with online dependencies when running Rspec offline?

I would like to run RSpec to test my code both when I'm connected to the web and when I'm not.

Unfortunately there are some tests in the application which are dep开发者_开发百科endent on having a live connection - email sending, Facebook integration etc.

Is there a best-practice way to create an online/offline testing environment or is this bad practice? Judging by how little I can find about this on the web I'm guessing the latter.


Normally in situations like that you would mock the parts of the code that connect outside your application. This allows you to test against the expected results from the service/system you are connecting to. It's also quicker to run tests.

There's a brief tutorial on mocking with rspec here but I'm sure you can find plenty yourself.

For testing that emails get sent there are other approaches if you are sending through ActionMailer. There's a section on that in the rails testing guide.

EDIT (in response to comment): You could put a method in TestHelper to only run tests when you are online. Something like:

def when_online
  if test_remote_connectivity
    yield
  else
    puts "Skipping test offline."
  end
end

Then you can call it like:

def test_facebook
  when_online do
    .....
  end
end

Not sure I entirely advocate it but it might do what you want!


You could use webmock inside the tests/specs you don't want connecting to the remote resource.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜