'无法在rails 4.1.8中加载此类文件 - factory_girl_rails(LoadError)'

时间:2015-06-10 07:02:36

标签: rspec factory-bot ruby-on-rails-4.1

运行rspec时,我收到以下错误:

/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'

我的gemfile.lock看起来像:

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

spec_helper.rb有一行require 'factory_girl_rails'。如果我在这里错过了什么,请告诉我。

1 个答案:

答案 0 :(得分:0)

factory_girl_rails是一个railtie gem,这意味着它将挂钩到rails生命周期并注册自己。所以你不需要隐含地要求它。事实上,你甚至不能要求它,这是你的问题中的错误原因。您需要做的就是将它包含在Gemfile中并捆绑它。然后你就可以获得factory_girl的生成器了。

为了将factory_girl用于rspec,您可以添加spec_helper.rb,如下所示:

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

取自factory_girl docs