【问题标题】:rspec: how can I continue test after first failurerspec:第一次失败后如何继续测试
【发布时间】:2013-09-16 13:04:53
【问题描述】:

我正在使用 rspec 对设备进行系统测试。该设备是模块化的,因此可以将任意数量的子设备连接到测试台。有很多地方我想编写测试,这些测试将遍历连接的子设备并在每个设备上运行相同的测试。

基本上,这就是我想要做的:

before(:all)   
  @tool = discover_sub_devices()
  @tool.sub_devices.should == exp_sub_device_list
end

describe "some sub device tests" do
  @tool.sub_devices.each do |dev|   
    it "should succeed at doing things" do
      dev.do_thing.should == success   
    end 
  end
end

不幸的是,这不起作用。在测试甚至运行之前,我收到错误说 @tool 是 nill 并且不包含类 sub_devices。所以测试在before(:all) 块运行之前被解析。

我可以让它工作的一种方法是将循环放在it 块内。像这样:

it "should succeed at doing things" do
  @tool.sub_devices.each do |dev| 
    dev.do_thing.should == success   
  end
end

这样做的问题是我真的想测试所有子设备,即使第一个失败了。我想查看究竟有多少子设备发生故障的报告。此代码将在一个失败时立即中断,而不是测试其余部分。

我意识到这可能不是 rspec 的正常用例,但如果我能做到这一点,它将非常方便我们的测试情况。

有什么建议吗?

【问题讨论】:

    标签: ruby loops testing rspec


    【解决方案1】:

    这里有一些写作技巧。

    最好避免使用before :all。最好避免在示例之外创建对象。

    describe "some sub device tests" do
    
      let(:tool) { discover_sub_devices }
    
      it "matches the sub device list" do
        tool.sub_devices.should be == expected_sub_devices_list
      end
    
      it "succeeds with all sub-devices" do
        failures = tool.sub_devices.reject{|d| d.do_thing == success}
    
        # option 1
        failures.should be_empty # will show just a yes/no result
    
        # option 2
        failures.size.should be == 0 # will show the number of failures
    
        # option 3
        failures.should be == [] # will display all sub-devices which fail
      end
    
    end
    

    【讨论】:

      【解决方案2】:

      您面临的问题是describe 块的主体会立即执行,而letbeforeit 块的主体会在以后执行。

      假设您不需要每次都重新发现设备,您可以按如下方式重构您的代码,消除before 调用:

      describe "some sub device tests" do
        tool = discover_sub_devices()
        it "should discover sub devices correctly" do
           tool.sub_devices.should == exp_sub_device_list
        end
        tool.sub_devices.each do |dev|   
          it "should succeed at doing things" do
            dev.do_thing.should == success   
          end 
        end
      end
      

      【讨论】:

      • 很好的建议,但我似乎无法让它发挥作用。 @tool 似乎总是为零。
      • 我认为问题在于 it 块内的 @tool 和 it 块外的 @tool 没有相同的范围:(
      • 很抱歉。我忘记了实例变量不适用于这种技术。查看更新的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2018-04-01
      • 2015-12-18
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多