Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec - undefined method 'let'

This is my rspec file:

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

When I run it, I get the following error:

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

My Gemfile has both 'rspec-rails' and 'capybara' gems. Anyone have any idea on how to fix this? Thanks

like image 918
SalmonKiller Avatar asked May 23 '14 02:05

SalmonKiller


Video Answer


2 Answers

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

you should never have a let block inside of a it block. It should be called from within a describe

like image 132
SSR Avatar answered Oct 19 '22 04:10

SSR


let is only defined within describe. You've used it within it

like image 6
Peter Alfvin Avatar answered Oct 19 '22 03:10

Peter Alfvin