Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec-rails and factory girl block in <top (required)>': undefined method `build'

I have a new Rails 4 project with FactoryGirl and rSpec. In my spec_helper.rb I have:

# lots of stuff
RSpec.configure do |config|
  # more stuff
  config.include FactoryGirl::Syntax::Methods
end

I also removed the rspec/autorun require in this file.

A simple spec:

require 'spec_helper'

describe User do
  build(:user)
end

with a simple factory:

FactoryGirl.define do
  factory :user do
    email     "[email protected]"
  end
end

Fails with the following message.

`block in <top (required)>': undefined method `build' for #<Class:0x007fd46d0e3848> (NoMethodError)

However, if I explicitly qualify build in the spec like this it passes:

require 'spec_helper'

describe User do
  FactoryGirl.build(:user)
end

What can I do so I don't have to add FactoryGirl every time?

like image 839
Steve Ross Avatar asked Jan 30 '14 23:01

Steve Ross


1 Answers

The methods passed to config.include are only included by RSpec within the it, let, before and after blocks, not at the top level of describe. Since that's where you generally need to put your setup and test logic, in practice it's not really an issue.

like image 126
Peter Alfvin Avatar answered Nov 12 '22 03:11

Peter Alfvin