Reputation: 878
Part of my command-line Ruby program involves checking if there is an internet connection before any commands are processed. The actual check in the program is trivial (using Socket::TCPSocket), but I'm trying to test this behaviour in Cucumber for an integration test.
The code:
def self.has_internet?(force = nil)
if !force.nil? then return force
begin
TCPSocket.new('www.yelp.co.uk', 80)
return true
rescue SocketError
return false
end
end
if has_internet? == false
puts("Could not connect to the Internet!")
exit 2
end
The feature:
Scenario: Failing to log in due to no Internet connection
Given the Internet is down
When I run `login <email_address> <password>`
Then the exit status should be 2
And the output should contain "Could not connect to the Internet!"
I obviously don't want to change the implementation to fit the test, and I require all my scenarios to pass. Clearly if there is actually no connection, the test passes as it is, but my other tests fail as they require a connection.
My question: How can I test for this in a valid way and have all my tests pass?
Upvotes: 5
Views: 1040
Reputation: 164
maybe a bit late for you :), but have a look at
https://github.com/mmolhoek/vcr-uri-catcher
I made this to test network failures, so this should do the trick for you.
Upvotes: 0
Reputation: 4245
You can stub your has_internet?
method and return false in the implementation of the Given the Internet is down
step.
YourClass.stub!(:has_internet?).and_return(false)
Upvotes: 4
Reputation: 13877
There are three alternative solutions I can think of:
TCPSocket.initialize
(or maybe Socket#connect
, if that's where it ends up) to pretend the internet is down.iptables
firewall rule to disable the internet, and have your test call the scriptLD_PRELOAD
on a specially written .so shared library that overrides the connect
C call. This is harder.Myself, I would probably try option 1, give up after about 5 minutes and go with option 2.
Upvotes: 1