我的命令行
Ruby程序的一部分涉及在处理任何命令之前检查是否存在Internet连接.程序中的实际检查是微不足道的(使用Socket :: Tcpsocket),但我试图在Cucumber中测试这种行为以进行集成测试.
代码:
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
功能:
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!"
我显然不想改变实现以适应测试,并且我要求所有场景都通过.显然,如果实际上没有连接,则测试按原样通过,但我的其他测试因需要连接而失败.
我的问题:如何以有效的方式测试这个并让我的所有测试通过?
解决方法
你可以存根你的has_internet吗?方法和返回false在执行给定的互联网是向下步骤.
YourClass.stub!(:has_internet?).and_return(false)