Comments (11)
@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.
@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.
@gladgit alright, strange! Could you paste...
- Your entire
spec/spec_helper.rb
file - Your entire Gemfile
- A full backtrace of the failure
Thanks! That should help me narrow it down a bit better.
from factory_bot_rails.
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.
=======================================================
# 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.
@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.
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.
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:
- Don't set
RAILS_ENV
tomacdev
in your shell, since that's messing with your test env. - Run your tests under the test environment by running
RAILS_ENV=test rspec spec/models/whatever_spec.rb
- Leave the explicit assignment of
RAILS_ENV
to test in the spec_helper.rb. - 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.
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.
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.
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)
- Invalid DateTime.now field on factory creation
- Multiple Tests Fail on Vanilla `bundle exec rake` HOT 2
- Model generation ignores --force-plural HOT 3
- Rails ignoring factory HOT 2
- Install fewer gems on Travis HOT 1
- Incompatibility with Rails 6.0.3? HOT 2
- Perform DB migration will make factory_bot looking for methods in the wrong place HOT 2
- Successive associations issue when migrating to rails 6.1 HOT 3
- Switch from Travis CI to GitHub Actions
- Duplicate factories problem HOT 5
- Automatically populate attributes and types when generating factories for existing models HOT 2
- Cannot create Factory with multiple traits got (ArgumentError Exception: wrong number of arguments (given 1, expected 0)) Ruby 3.0, Rails 6.1.72, factory_bot_rails 6.1.0 HOT 2
- Dead link in CONTRIBUTING.md HOT 2
- Release new version to allow upgrade to factory_bot 6.3.0 HOT 4
- The railtie included in #419 breaks compatibility with some gems "undefined method `config' for nil:NilClass" Factory Bot > 6.2.0 HOT 5
- 6.3.0: `NoMethodError: undefined method '<' for nil:NilClass` when using a factory with traits HOT 20
- undefined method `config' for nil:NilClass after upgrading to 6.4.0 HOT 16
- Ability to disable reject_primary_key_attributes per factory HOT 11
- RubyGems is not linking the CHANGELOG file
- gemspec is missing required_ruby_version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from factory_bot_rails.