Rails 4.1.8中的“无法加载此类文件-factory_girl_rails(LoadError)" [英] 'cannot load such file -- factory_girl_rails (LoadError)' in rails 4.1.8

查看:62
本文介绍了Rails 4.1.8中的“无法加载此类文件-factory_girl_rails(LoadError)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行rspec时,出现以下错误:

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'

我的gemfile.lock看起来像:

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具有require 'factory_girl_rails'行.请让我知道我是否错过了这里.

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

推荐答案

factory_girl_railsrailtie gem,这意味着它将挂接到rails生命周期并进行注册.因此,您不需要隐式要求它.实际上,您甚至不能要求使用它,这是问题中的错误原因.您需要做的只是将其包含在Gemfile中并捆绑在一起.然后,您将获得factory_girl的生成器.

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.

为了将factory_girl与rspec一起使用,可以按如下所示在spec_helper.rb中添加:

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

它取自 factory_girl文档

这篇关于Rails 4.1.8中的“无法加载此类文件-factory_girl_rails(LoadError)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆