Rspec - 未定义的方法'让'

时间:2014-05-23 02:50:44

标签: ruby-on-rails ruby rspec capybara

这是我的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'宝石。任何人都知道如何解决这个问题? 感谢

2 个答案:

答案 0 :(得分:29)

describe "Birds" do
  let(:bird) { FactoryGirl.create(:bird) }

你永远不应该在let块内有it块。它应该在describe

内调用

答案 1 :(得分:6)

let仅在describe内定义。您已在it

中使用过它