加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 包含带变量的Rspec示例

发布时间:2020-12-17 02:20:44 所属栏目:百科 来源:网络整理
导读:我有几组rspec,都包含一些共享示例.如果原始规范有一些变量集,我希望这些共享示例包含其他共享示例.基本上这就是我想要做的. 例: 文件:spec / test_spec.rb describe 'some thing' do let(:some_feature) { true } describe 'some tests' do include_examp
我有几组rspec,都包含一些共享示例.如果原始规范有一些变量集,我希望这些共享示例包含其他共享示例.基本上这就是我想要做的.

例:

文件:spec / test_spec.rb

describe 'some thing' do
  let(:some_feature) { true }

  describe 'some tests' do
    include_examples "shared_tests" 
  end
end

文件规范/共享/ 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`

有没有办法做到这一点?我想也许我可以在before(:all)块中定义@some_feature,然后在shared_examples中使用if @some_feature,但这总是为零.

解决方法

重写答案,使其更清晰:

你有这个:

文件:spec / test_spec.rb

describe 'some thing' do
  let(:some_feature) { true }

  describe 'some tests' do
    include_examples "shared_tests" 
  end
end

文件规范/共享/ 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_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

这一切都很好用:-)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读