ruby – 在Cucumber测试中模拟没有Internet连接的最佳方法是什么
发布时间:2020-12-17 02:31:52 所属栏目:百科 来源:网络整理
导读:我的命令行 Ruby程序的一部分涉及在处理任何命令之前检查是否存在Internet连接.程序中的实际检查是微不足道的(使用Socket :: TCPSocket),但我试图在Cucumber中测试这种行为以进行集成测试. 代码: def self.has_internet?(force = nil) if !force.nil? then
我的命令行
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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |