我有几组rspec,都包含一些共享示例。如果原始规范有一些变量集,我希望这些共享示例包含其他共享示例。基本上这就是我想要做的。
示例:
档案:spec/test_spec.rb
describe 'some thing' do
let(:some_feature) { true }
describe 'some tests' do
include_examples "shared_tests"
end
end
档案spec/shared/shared_tests.rb
shared_examples_for "shared_tests" do
include_examples "feature_specific_tests" if some_feature
end
正如预期的那样,这会引发如下错误:
undefined local variable or method `some_feature`
有办法做到这一点吗?我想也许我可以在@some_feature
块中定义before(:all)
,然后在if @some_feature
中使用shared_examples
,但这始终是nil
。
答案 0 :(得分:3)
重写答案,使其更清晰:
你有这个:
文件:spec / test_spec.rb
describe 'some thing' do
let(:some_feature) { true }
describe 'some tests' do
include_examples "shared_tests"
end
end
文件规范/ shared / shared_tests.rb
shared_examples_for "shared_tests" do
include_examples "feature_specific_tests" if some_feature
end
将其更改为:
文件:spec / test_spec.rb
describe 'some thing' do
describe 'some tests' do
include_examples "shared_tests" do
let(:some_feature) { true }
end
end
end
文件规范/ shared / shared_tests.rb
shared_examples "shared_tests" do
if some_feature
it_should_behave_like "feature_specific_tests"
end
# rest of your tests for shared example group
# 'a logged in registered user goes here
end
这一切都很好用: - )