rspec 未定义的局部变量已被`let`

Ala*_*DRE 1 ruby rspec let

我需要测试一个文件是否包含特定的单词列表。

所以我在描述块中使用 let :

let (:test_rb_structure) { %w(nom, description, prix, rdv, validation, heure, creation) }
Run Code Online (Sandbox Code Playgroud)

我在同一个 describe bloc 中这样称呼它:

describe 'in app/controllers/api/v1/comptes.rb' do
  subject { file('app/controllers/api/v1/comptes.rb') }
  it { is_expected.to exist }
  # Test structure is respected
  test_rb_structure.each do |structure|
    it { is_expected.to contain(structure) }
  end
end
Run Code Online (Sandbox Code Playgroud)

我有这个错误:

undefined local variable or method `test_rb_structure'
Run Code Online (Sandbox Code Playgroud)

怎么了 ?我想不通。

the*_*ler 5

using 定义的变量let仅在 example ( it) 块内可用。因此,您必须执行以下操作:

describe 'in app/controllers/api/v1/comptes.rb' do
  let (:test_rb_structure) { %w(nom, description, prix, rdv, validation, heure, creation) }

  subject { file('app/controllers/api/v1/comptes.rb') }

  it { is_expected.to exist }

  it 'respects the test structure' do
    # Notice that `test_rb_structure` is used _inside_ the `it` block.
    test_rb_structure.each do |structure|
      expect(subject).to contain(structure)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)