运行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_rails是railtie 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中的“无法加载此类文件
发布评论