[英]Is there any way to annotate a test in RSpec?
我经常在RSpec中遇到问题,当我想遍历一组条件时,我想在测试输出中打印一些内容,以说明我在这些条件中的位置。 例如:
it "should save & serialize all attributes" do
@raw_user_attrs.each do |attr_name, value|
expect( @twitter_user.send(attr_name) ).to eq value
expect( @twitter_user.raw_attrs[attr_name]).to eq value
end
end
该测试的问题在于,当它失败时,您看不到哪个属性导致它失败,输出看起来像:
1) TwitterUser Raw user loading: data loading should save & serialize all attributes except created_at (auto test)
Failure/Error: expect( @twitter_user.send(attr_name) ).to eq value
expected: false
got: nil
(compared using ==)
# ./spec/models/twitter_user_spec.rb:38:in `block (5 levels) in <top (required)>'
# ./spec/models/twitter_user_spec.rb:37:in `each'
# ./spec/models/twitter_user_spec.rb:37:in `block (4 levels) in <top (required)>'
而且此输出的问题是我不知道它实际针对哪个属性失败。
如果我可以将测试包装在循环中,那就太好了,即
raw_user_attrs.except(:created_at).each do |attr_name, value|
it "should save & serialize #{attr_name}" do
@twitter_user.save
expect( @twitter_user.send(attr_name) ).to eq value
expect( @twitter_user.raw_attrs[attr_name]).to eq value
end
end
这会将变量打印到实际的测试名称中。 但是,变量在测试范围之外不可用,因此不起作用。
有什么方法可以打印出测试失败时正在发生的事情的其他信息?
除了运算符匹配(例如should be > 1
)之外,您可以轻松地做到这一点。 从Relish应用程序文档中 :
expect(array).to be_empty, "expected empty array, got #{array.inspect}"
因此,您可以添加预期的消息。
别的东西,你可以在某些情况下做的是围绕着数据环路,然后执行it
■循环内,给他们每一个好名字。 例:
[:method1, :method2].each do |method_sym|
it "doesn't fail when calling method: #{method_sym}" do
expect { @object.send(method_sym) }.to_not raise_error
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.