Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 860
Daniel Romero Avatar asked Feb 01 '13 12:02

Daniel Romero


Video Answer


1 Answers

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.

like image 70
Chris Salzberg Avatar answered Sep 28 '22 09:09

Chris Salzberg