【问题标题】:Rspec - undefined method 'let'Rspec - 未定义的方法“让”
【发布时间】:2014-05-23 02:50:43
【问题描述】:

这是我的 rspec 文件:

require 'spec_helper'

describe "Birds" do
  before { visit birds_path }
  it "should have the right title" do
    expect(page).to have_content("Approved Birds")
  end
  it "should contain the bird's name, genus, species" do
    let(:bird) { FactoryGirl.create(:bird) }
    expect(page).to have_content("#{bird.name}")
    expect(page).to have_content("#{bird.genus}")
    expect(page).to have_content("#{bird.species}")
  end
end

当我运行它时,我收到以下错误:

Failure/Error: let(:bird) { FactoryGirl.create(:bird) }
 NoMethodError:
   undefined method `let' for #<RSpec::Core::ExampleGroup::Nested_1:0xad87f84>

我的 Gemfile 有 'rspec-rails' 和 'capybara' gems。任何人都知道如何解决这个问题? 谢谢

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara


    【解决方案1】:
    describe "Birds" do
      let(:bird) { FactoryGirl.create(:bird) }
    

    你不应该在it 块内有一个let 块。应该从 describe 中调用它

    【讨论】:

    • 我想补充一点,你不应该在before 块中使用let(在这种情况下你应该使用let!)。 let 块的唯一位置是在 describe 块内。在kolosek.com/rspec-let-vs-before 上阅读更多相关信息
    【解决方案2】:

    let 仅在describe 中定义。你在it中使用过它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多