Daniel Romero
Daniel Romero

Reputation: 1588

RSpec let error

In my spec/models/user_spec.rb I have the next:

require 'spec_helper'

describe User do

  before { 5.times { FactoryGirl.create(:sport) } }
  before { let(:user) { FactoryGirl.create(:user) } }

  ...

end

But when running my user_spec.rb I'm getting this error:

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

Which is weird because my specs have been working until now, and I haven't changed anything...

Here is my gemfile: https://gist.github.com/4690919

Here is my spec_helper.rb: https://gist.github.com/4690935

I'd appreciate any help, thanks

Upvotes: 6

Views: 4569

Answers (1)

Chris Salzberg
Chris Salzberg

Reputation: 27374

I don't know why it was working before, but you should never have a let block inside of a before block. It should be called from within a describe (or context) block, like this:

describe User do
  let(:user) { FactoryGirl.create(:user) }
  before { 5.times { FactoryGirl.create(:sport) } }
  ....

See the documentation for details.

Upvotes: 17

Related Questions