'cannot load such file — factory_girl_rails (LoadError)' in rails 4.1.8

ⅰ亾dé卋堺 提交于 2019-12-13 06:38:06

问题


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



来源:https://stackoverflow.com/questions/30749280/cannot-load-such-file-factory-girl-rails-loaderror-in-rails-4-1-8

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