Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError: uninitialized constant FactoryGirl #60

Closed
ghost opened this issue May 28, 2012 · 11 comments
Closed

NameError: uninitialized constant FactoryGirl #60

ghost opened this issue May 28, 2012 · 11 comments

Comments

@ghost
Copy link

ghost commented May 28, 2012

It appears that factory_girl_rails is supposed to automatically load itself, but it does not in my environment. The only installation instructions say to add factory_girl_rails to the gemfile (in the test group). But this does not make FactoryGirl available to me.

I get the error "NameError: uninitialized constant FactoryGirl"

When I require 'factory_girl' in my spec_helper.rb file, that error goes away, and I get a new error: "ArgumentError: Factory not registered: user", even though the :user factory has been created.

When I add "FactoryGirl.find_definitions" to my spec_helper.rb file, then everything works correctly.

I am using factory_girl-3.3.0, factory_girl_rails-3.3.0, rspec-2.8.0, rails-3.0.4, ruby-1.9.2

If factory_girl_rails is supposed to automatically load itself, then if fails in my environment. If it isn't, then perhaps the installation instructions should be updated to include instructions on requiring the necessary files.

@joshuaclayton
Copy link
Contributor

@gladgit Sorry you're having issues.

Are you using Spork or any sort of similar gem?

I also want to confirm that you have not added require: false to the end of the gem declaration in your Gemfile, since that'll not load factory_girl_rails when you want it to.

Finally, can you confirm that you're running your tests in the test environment (or whatever environment the group is from your Gemfile)? If the Rails environment is different from the group you've declared factory_girl_rails falls within, it won't load either.

Thanks!

@ghost
Copy link
Author

ghost commented May 29, 2012

@joshuaclayton Thanks for looking into this.

To answer your questions, I am not using Spork or similar. I have not added require: false

factory_girl_rails is in the :test group in my Gemfile. And I run rake spec from my development environment, which is according to the Rspec documentation.

@joshuaclayton
Copy link
Contributor

@gladgit alright, strange! Could you paste...

  1. Your entire spec/spec_helper.rb file
  2. Your entire Gemfile
  3. A full backtrace of the failure

Thanks! That should help me narrow it down a bit better.

@ghost
Copy link
Author

ghost commented May 30, 2012

Quick question first, does FactoryGirl work with a custom development environment? I am not using the standard ':development' environment. I have set up a custom development environment for Rails with its own environment file and RAILS_ENV setting.

@ghost
Copy link
Author

ghost commented May 30, 2012

=======================================================
# Rspec run
=======================================================

~/work/portal> rake spec
F.

Failures:

  1) User should have privacy on by default
     Failure/Error: user = FactoryGirl.create(:user)
     NameError:
       uninitialized constant FactoryGirl
     # ./spec/models/user_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00703 seconds
2 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:5 # User should have privacy on by default
rake aborted!
/Users/xxxxx/.rvm/rubies/ruby-1.9.2-p180/bin/ruby -S rspec ./spec/models/user_spec.rb ./spec/requests/copy_products_spec.rb failed

Tasks: TOP => spec
(See full trace by running task with --trace)


=======================================================
# spec/spec_helper.rb file
=======================================================

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require File.dirname(__FILE__) + "/request_macros"

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  #config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Convenience methods for request specs
  config.include(RequestMacros, :type => :request)

end


=======================================================
# Gemfile
=======================================================

source 'http://rubygems.org'

gem 'rails', '3.0.4'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'mysql2'

gem 'declarative_authorization', '>=0.5.2'

# pagination gem
gem 'will_paginate', '>=3.0.pre2'

#need sanitize gem to clean up html
gem 'sanitize'  

#application level monitoring
gem 'newrelic_rpm'  

gem 'rails-dev-boost', :git => 'git://github.com/thedarkone/rails-dev-boost.git', :require => 'rails_development_boost', :group => [:development, :macdev]

gem 'rubyzip'

# Gem for writing and deploying cron jobs
gem 'whenever', :require => false

# Gem for searching and sorting
gem 'meta_search'

# Avoid SQL fragments in Rails code
gem 'meta_where'

# For testing
gem "rspec-rails", :group => [:test, :development, :macdev]
group :test do
  gem "factory_girl_rails", "~> 3.0"
  gem "capybara"
  gem "launchy"
end

@joshuaclayton
Copy link
Contributor

@gladgit Did you do something like export RAILS_ENV=macdev in your shell?

Actually, even better, try this: change ENV["RAILS_ENV"] ||= 'test' to ENV["RAILS_ENV"] = 'test' in your spec/spec_helper.rb file and try again. Let me know what you see. Thanks!

@ghost
Copy link
Author

ghost commented Jun 1, 2012

Yes, I explicitly export RAILS_ENV=macdev in my shell.

I made the change to ENV["RAILS_ENV"] = 'test' in the spec/spec_helper.rb file and it worked. FactoryGirl loads automatically.

I know the spec/spec_helper.rb file is not part of FactoryGirl, but do you know of any gotchas with leaving ENV["RAILS_ENV"] = 'test'" in there? Is the spec_helper.rb file run in any other environments? Seems like it should only run in a test environment anyway.

Thanks for all your help.

@joshuaclayton
Copy link
Contributor

Any reason for setting RAILS_ENV in your shell?

The reason FactoryGirl isn't loading is because you've declared it in your Gemfile to only be available in the test environment, but if your shell sets it to something different than test, then it's not going to get loaded (that's the issue you were seeing). The reason why assigning RAILS_ENV in your spec_helper.rb file worked is because it forced RAILS_ENV to be test; the or-equals saw the env you set in your shell and was using macdev instead.

So, there are a couple solutions:

  1. Don't set RAILS_ENV to macdev in your shell, since that's messing with your test env.
  2. Run your tests under the test environment by running RAILS_ENV=test rspec spec/models/whatever_spec.rb
  3. Leave the explicit assignment of RAILS_ENV to test in the spec_helper.rb.
  4. Change your test group to also include the macdev group.

I've ordered these by best to worst fixes. The easiest and cleanest thing to do, assuming you're not checking for a macdev environment all over your code (which would be bad!) is just remove setting RAILS_ENV in your shell. If you want to leave it in there, then just prefix all your test commands (rake, rspec, cucumber with the explicit RAILS_ENV=test like the example above. That'll give you command-level overriding of the env. The third solution is to just leave the change to spec_helper.rb. This isn't intuitive and may cause other people frustration, so I'd recommend against it. Finally, you can change your test group to also include macdev. This is dangerous. Your test code will run in the environment I'm assuming you're actively developing in, which means you'll be clobbering the database during your tests AND you won't be able to reliably interact with your localhost server because data will be added/removed during tests.

@ghost
Copy link
Author

ghost commented Jun 1, 2012

I set RAILS_ENV as a solution for having multiple developers on a project, each with their own development database and settings, meaning their own database.yml file. So probably the easiest thing to do is use another solution for that problem, and don't fiddle with RAILS_ENV.

But I'm curious why the spec_helper.rb file uses the or-equals to set RAILS_ENV. Presumably that file sets up the test environment so I imagine you would always want RAILS_ENV to be set to 'test' anyway.

Thanks again for your help.

@jongrover
Copy link

I had the same issue but I noticed I had spork running before I included FactoryGirl gem in test environment. By simply restarting spork and running rspec again with fresh spork it fixed the issue.

@spinlock99
Copy link

I just got to this thread after much googling and -- just like @johgrover -- all I needed to do was restart zeus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants