1
votes

I am getting "nameError: uninitialized constant" error when running rspecs with FactoryGirl but when I call FactoryGirl.build inside the console by running, rails c test, it recognizes FactoryGirl properly.

Here is the output from rspec:

Failures:

1) create user test
 Failure/Error: FactoryGirl.build(:user)

 NameError:
   uninitialized constant User
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `block in constantize'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:26:in `build_class'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:37:in `run'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
 # ./spec/model/user_spec.rb:7:in `block (2 levels) in <top (required)>'

 Finished in 0.001 seconds (files took 0.31845 seconds to load)

1 example, 1 failure

/spec/spec_helper.rb:

require 'factory_girl_rails'

RSpec.configure do |config|

  config.include FactoryGirl::Syntax::Methods
  config.before do
    FactoryGirl.find_definitions
  end
  ...
end

/spec/rails_helper.rb:

require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'factory_girl_rails'

RSpec.configure do |config|

  config.include FactoryGirl::Syntax::Methods

  ...

end

/spec/model/user_spec.rb:

 require 'spec_helper'

 # Create factory de user
 describe "create user" do
  it "test" do
    u = FactoryGirl.build(:user)
  end
end

/Gemfile:

group :development, :test do
  gem 'rspec-rails'
  gem 'guard-rspec'
  gem 'capybara'
  gem 'factory_girl_rails'

end
2

2 Answers

3
votes

To resolve NameError:

  • You need to move configuration of FactoryGirl to /spec/rails_helper.rb from /spec/spec_helper.rb.

  • You need to replace spec_helper with rails helper in /spec/model/user_spec.rb.

Please, try it.

1
votes

Are you using rspec 3 by any chance? Starting from rspec 3, you need require 'rails_helper' instead of spec_helper.

See https://github.com/rspec/rspec-rails#upgrade-note