gpt4 book ai didi

rspec - FactoryGirl:工厂未注册

转载 作者:行者123 更新时间:2023-12-03 14:43:19 25 4
gpt4 key购买 nike

我从事的项目结构如下:

projects/warehouse/core
projects/warehouse/products/bottles
projects/warehouse/products/boxes


在这个项目中,应用程序逻辑,gem等都在 core应用程序中。我已经为rspec设置了 boxes,例如:

projects/warehouse/products/boxes/spec
/factories
/models


factories目录包含 cubics.rb

FactoryGirl.define do
factory :cubic
id 1
dimension 12
end
end


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


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

module Boxes
class Cubic < BoxExBase
self.table_name = 'containers'
#validation stuff goes here
end
end


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

FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
FactoryGirl.find_definitions


core中的gemfile在开发测试组中包含 gem 'factory_girl_rails'。我什至试图让工厂提出一个错误,但这甚至没有发生。因此,工厂似乎甚至没有被加载。我需要怎么做才能将该工厂加载并注册?

最佳答案

您需要帮助虚拟应用程序(或Rails控制台),因为FactoryGirl将查找规格/虚拟的Rails.root。

因此,例如,从spec / dummy中启动的控制台执行以下操作:

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


对于您的特殊情况,您需要更改实际位置,但是如果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.


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

├── 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


我只在我的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'

关于rspec - FactoryGirl:工厂未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19299078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com