문제

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 dependent 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top