问题描述
factory_girl
和 factory_girl_rails
Ruby Gems之间有区别吗?我在RSpec测试中经常出现错误问题:未初始化的常量FactoryGirl(NameError)".
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?
我的spec_helper文件同时具有:
My spec_helper file has both:
require 'factory_girl'
require 'factory_girl_rails'
我的Gemfile具有:
My Gemfile has:
gem 'factory_girl_rails'
这是完整的错误:
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$
推荐答案
您只需要:
group :development, :test do
gem "rspec-rails"
gem "factory_girl_rails"
end
因为factory_girl_rails自动合并了factory_girl gem并添加了对Rails的支持.
because factory_girl_rails automatically incorporates the factory_girl gem and adds support for Rails.
这些gems属于测试组和开发组,因为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.
无需在您的specs/spec_helper.rb文件中添加factory_girl或factory_girl_rails.
There's no need to add factory_girl or factory_girl_rails to your specs/spec_helper.rb file.
这篇关于factory_girl和factory_girl_rails Ruby Gems之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!