Coder Social home page Coder Social logo

Comments (11)

joshuaclayton avatar joshuaclayton commented on June 18, 2024

@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!

from factory_bot_rails.

 avatar commented on June 18, 2024

@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.

from factory_bot_rails.

joshuaclayton avatar joshuaclayton commented on June 18, 2024

@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.

from factory_bot_rails.

 avatar commented on June 18, 2024

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.

from factory_bot_rails.

 avatar commented on June 18, 2024
=======================================================
# 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

from factory_bot_rails.

joshuaclayton avatar joshuaclayton commented on June 18, 2024

@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!

from factory_bot_rails.

 avatar commented on June 18, 2024

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.

from factory_bot_rails.

joshuaclayton avatar joshuaclayton commented on June 18, 2024

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.

from factory_bot_rails.

 avatar commented on June 18, 2024

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.

from factory_bot_rails.

jongrover avatar jongrover commented on June 18, 2024

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.

from factory_bot_rails.

spinlock99 avatar spinlock99 commented on June 18, 2024

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

from factory_bot_rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.