rspec-rails and factory girl block in <top (required)>': undefined method `build'
factory-bot, rspec, ruby-on-rails
Solution
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.
Problem
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 "somename@someplace.com" 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?