Difference between factory_girl and factory_girl_rails Ruby Gems

北慕城南 提交于 2020-01-03 05:18:31

问题


Is there a difference between factory_girl and factory_girl_rails Ruby Gems? I have a recurring problem with an error in RSpec Testing: "uninitialized constant FactoryGirl (NameError)".

Somebody told me that there is a difference between the two (this is really confusing) and one needs the other to work or something along those lines?

My spec_helper file has both:

require 'factory_girl'
require 'factory_girl_rails'

My Gemfile has:

gem 'factory_girl_rails'

Here is the full error:

uninitialized constant FactoryGirl (NameError)
    from /srv/homes/rvm/gems/ruby-2.0.0-p247@global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load'
    from /srv/homes/rvm/gems/ruby-2.0.0-p247@global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /srv/homes/rvm/gems/ruby-2.0.0-p247@global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each'
    from /srv/homes/rvm/gems/ruby-2.0.0-p247@global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /srv/homes/rvm/gems/ruby-2.0.0-p247@global/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run'
    from /srv/homes/rvm/gems/ruby-2.0.0-p247@global/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run'
    from /srv/homes/rvm/gems/ruby-2.0.0-p247@global/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
****@epi-stu-hut-shell3:~/projects/project4/spec/factories$ 

回答1:


You only need:

group :development, :test do
  gem "rspec-rails"
  gem "factory_girl_rails"
end

because factory_girl_rails automatically incorporates the factory_girl gem and adds support for Rails.

These gems go in both test and development groups because Rails generators will create stub files in development and of course they are needed in the test environment.

There's no need to add factory_girl or factory_girl_rails to your specs/spec_helper.rb file.




回答2:


Add factory_girl_rails to your Gemfile under test group

group :test, :development do
  gem "factory_girl_rails"
end

then do NOT do a require in spec_helper, it should get loaded automatically during tests; use bundle exec if needed when running rspec



来源:https://stackoverflow.com/questions/22865256/difference-between-factory-girl-and-factory-girl-rails-ruby-gems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!