微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby – 在Cucumber测试中模拟没有Internet连接的最佳方法是什么?

我的命令行 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐