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)

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...