FactoryGirl:工厂未注册

Cit*_*enX 16 rspec ruby-on-rails-3.2 factory-bot

我的项目结构如下:

projects/warehouse/core
projects/warehouse/products/bottles
projects/warehouse/products/boxes
Run Code Online (Sandbox Code Playgroud)

在这个项目中,应用程序逻辑,宝石等都在core应用程序中.我boxes为rspec设置了这样的:

projects/warehouse/products/boxes/spec
    /factories
    /models
Run Code Online (Sandbox Code Playgroud)

factories目录包含cubics.rb:

FactoryGirl.define do
  factory :cubic
    id 1
    dimension 12
  end
end
Run Code Online (Sandbox Code Playgroud)

models目录包含cubic_spec.rb:

require 'spec_helper'

describe Boxes::Cubic do
  it "has a valid factory" do
    FactoryGirl.create(:cubic).should be_valid
  end
end
Run Code Online (Sandbox Code Playgroud)

Cubic模型位于products/boxes/app/models/boxes/cubic.rb.

module Boxes
  class Cubic < BoxExBase
    self.table_name = 'containers'
    #validation stuff goes here
  end
end
Run Code Online (Sandbox Code Playgroud)

简单明了.当我执行时,rspec ../products/boxes/spec/models/cubic_spec.rb我得到了ArgumentError:Factory未注册:cubic.我已经尝试在spec_helper.rb中要求factory_girl_rails.我试过修改spec_helper.rb w /

FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
FactoryGirl.find_definitions
Run Code Online (Sandbox Code Playgroud)

core包含gem 'factory_girl_rails'在开发,测试组中的gemfile .我甚至试图让工厂提出错误,但这甚至都没发生.因此,工厂似乎甚至没有加载.如何加载和注册此工厂需要做什么?

Ash*_*eri 14

你需要帮助虚拟应用程序(或rails控制台),因为FactoryGirl将查看Rails.root,这是spec/dummy.

所以,从spec/dummy启动的控制台中举例来说就是这样做:

FactoryGirl.definition_file_paths = %w(../factories)
FactoryGirl.find_definitions
# You can use this line to see what factories are loaded
# FactoryGirl.factories
Run Code Online (Sandbox Code Playgroud)

对于您的特定情况,您需要更改实际位置,但如果您在哪里,如果FactoryGirl没有找到您的工厂,那是因为它不知道在哪里看.

具体来说:这就是我所做的.我的需求如下 - 我需要从Gem根目录运行rspec.但我还需要能够从嵌入式虚拟项目中运行rails控制台并访问Factory girl以便在开发期间方便地创建测试对象.

TLDR将此代码添加到虚拟应用程序的application.rb中

 console do
   FactoryGirl.definition_file_paths << Pathname.new("../factories")
   FactoryGirl.definition_file_paths.uniq!
   FactoryGirl.find_definitions
 end

??? spec
?   ??? dummy
?   ?   ??? app
?   ?   ??? bin
?   ?   ??? config
?   ?   ?   ??? environments
?   ?   ?   ??? initializers
?   ?   ?   ??? locales
?   ?   ?   ??? application.rb << This is the file to add the lines in.
Run Code Online (Sandbox Code Playgroud)

我的应用程序布局的相关部分看起来像

??? app
??? bin
??? config
??? lib
??? spec
?   ??? dummy
?   ?   ??? app
?   ?   ??? bin
?   ?   ??? config
?   ?   ?   ??? environments
?   ?   ?   ??? initializers
?   ?   ?   ??? locales
?   ?   ?   ??? application.rb
?   ?   ?   ??? boot.rb
?   ?   ?   ??? database.yml
?   ?   ?   ??? environment.rb
?   ?   ?   ??? routes.rb
...
?   ??? factories
?   ?   ??? models
?   ?       ??? api_requests.rb
?   ??? lib
?   ?   ??? controllers
?   ?   ?   ??? controller_spec.rb
?   ?   ?   ??? session__spec.rb
?   ?   ??? my_spec.rb
?   ??? support
?   ??? vcr
?   ??? spec_helper.rb

??? Gemfile
??? Gemfile.lock
??? mygem.gemspec  << this is the file to include the factory-girl-rails gem
Run Code Online (Sandbox Code Playgroud)

我只在我的gemspec中包含了factory-girl-rails gem,只在那个位置.我的Gemfile中没有任何内容,虚拟应用程序的Gemfile中没有任何内容.

  s.add_development_dependency 'rspec-rails'
  s.add_development_dependency 'capybara'
  s.add_development_dependency 'factory_girl_rails'
  s.add_development_dependency 'shoulda'
  s.add_development_dependency 'vcr'
  s.add_development_dependency 'byebug'
  s.add_development_dependency 'better_errors'
  s.add_development_dependency 'binding_of_caller'
Run Code Online (Sandbox Code Playgroud)


Cit*_*enX 11

这是我们如何解决它.正如@Vinh Tran建议的那样,factory_girl_rails确实需要在Gemfile您正在处理的应用程序中:

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
end
Run Code Online (Sandbox Code Playgroud)

此外,虚拟应用程序需要位于/spec目录下,尽管此时它似乎没有填充实际模型等.

最后,rspec命令需要从boxes目录而不是core目录发出.所有这些结合在一起使我们能够成功注册工厂并成功进行测试.