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

rspec undefined local variable that have been `let`

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

所以我在 describe bloc 中使用 let :

let (:test_rb_structure) { %w(nom, description, prix, rdv, validation, heure, creation) }

我在同一个 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

我遇到了这个错误:

undefined local variable or method `test_rb_structure'

怎么了?我想不通。

使用 let 定义的变量仅在示例 (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