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

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 deleted rspec/autorun require in this file.

Simple specification:

 require 'spec_helper' describe User do build(:user) end 

with a simple factory:

 FactoryGirl.define do factory :user do email " somename@someplace.com " end end 

The following message failed to complete.

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

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

 require 'spec_helper' describe User do FactoryGirl.build(:user) end 

What can I do, I do not need to add FactoryGirl every time?

+6
source share
1 answer

Methods passed to config.include are only included by RSpec in the it , let , before and after blocks, and not at the top level describe . Since then, where you generally need to put your test logic and settings, in practice this is not a problem.

+10
source

All Articles