4

我有几组 rspecs,它们都包含一些共享示例。如果原始规范有一些变量集,我希望这些共享示例包含其他共享示例。基本上这就是我想要做的。

例子:

文件: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

4

1 回答 1

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

这一切都会很好地工作:-)

于 2013-03-23T02:09:15.943 回答