'cannot load such file — factory_girl_rails (L

2019-09-18 02:23发布

问题:

While running rspec, I am getting the following error:

/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require': cannot load such file -- factory_girl_rails (LoadError) from /Users/radhikabhatt/.rbenv/versions/2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire'
from /Users/radhikabhatt/Desktop/work/cl_portalmiudla/spec/spec_helper.rb:19:in <top (required)>' from /Users/radhikabhatt/.rbenv/versions/2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire'
from /Users/radhikabhatt/.rbenv/versions/2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

My gemfile.lock looks like:

factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)

spec_helper.rb has the line require 'factory_girl_rails'. Please let me know if I missed something here.

回答1:

factory_girl_rails is a railtie gem which means it will hook into the rails lifecycle and register itself. So you don't need to require it implicitly. In fact you even cannot require it which is the cause of error in your question. All you need to do is just include it in the Gemfile and bundle it. Then you get generators for factory_girl along with it.

In order to use factory_girl with rspec, you can add in you spec_helper.rb as follow:

# RSpec
# spec/support/factory_girl.rb
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

It is taken from factory_girl docs