我正在使用 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 为空并且不包含类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 的正常用例,但如果我能让这项工作正常进行,对于我们的测试情况将非常方便。
有什么建议么?
以下是一些编写此内容的技巧。
最好避免使用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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)