【发布时间】:2017-01-30 07:25:13
【问题描述】:
我有一个返回结果集的过程,我想使用 rspec 测试其有效性。该过程将根据参数返回不同的结果,但有很多示例对所有参数都通用,因此我想创建一组可以针对所有参数运行的通用示例。
我知道首选的做法是使用 let 来构建结果。问题是每个过程需要一两分钟才能生成结果,我可能有 30 个示例。使用基于不同参数的所有排列,我运行了大约 500 个示例。如果我必须为每个示例重新构建结果,测试将运行超过一天。
因此,我在 before(:all) 块中构建结果并将其分配给类似这样的属性:
RSpec.describe 'Test Description' do
attr_reader :result
before(:all)
@result = build_result({some_parameters})
end
context 'Some context' do
it 'Looks lik a result' do
expect(result.something).to ...
end
it 'Feels lik a result' do
expect(result.something).to ...
end
end
end
也许有比使用属性更好的方法。我想做这样的事情:
RSpec.describe 'Test Description' do
attr_reader :result
before(:all)
@result = build_result({some_parameters})
end
context 'Some context' do
it_behaves_like "A result" result
end
end
在此上下文中使用属性失败。有没有其他方法可以做到这一点?
【问题讨论】:
标签: ruby rspec parameter-passing