Ruby-on-rails ArgumentError:工厂未注册
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24078768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArgumentError: Factory not registered
提问by fkoessler
I am trying to get factory girl to run with rspec in my rails 4.1.1 app.
我试图让工厂女孩在我的 rails 4.1.1 应用程序中使用 rspec 运行。
Problem is when I run rspecin my command line, i get Failure/Error: verse = build(:verse) ArgumentError: Factory not registered: verse.
问题是当我rspec在命令行中运行时,我得到Failure/Error: verse = build(:verse) ArgumentError: Factory not registered: verse.
I am at loss because I checked the factory girl getting started page and many answers here on SO andI still can't fix this issue.
我不知所措,因为我检查了工厂女孩入门页面和 SO 上的许多答案,但我仍然无法解决这个问题。
in my Gemfile:
在我的 Gemfile 中:
gem 'rails', '4.1.1'
group :development, :test do
gem 'rspec-rails'
gem "factory_girl_rails"
end
my spec_helper.rb file:
我的 spec_helper.rb 文件:
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec/controllers/api/verses_controller_spec.rb
规格/控制器/api/verses_controller_spec.rb
describe "API Controller" do
describe "show a verse" do
it "should return status 200" do
verse = build(:verse)
get :show, id: verse.id
expect(response).to have_http_status(200)
end
it "should return json object" do
verse = build(:verse)
get :show, id: verse.id
JSON.parse(response.body).should == {'id' => verse.id}
end
end
end
spec/factories/verses.rb
规格/工厂/verses.rb
FactoryGirl.define do
factory :verse do
line1 "A beautiful verse I stand"
end
end
Why isn't my factory loading properly? Files in the spec/factories folder are supposed to get loaded automatically.
为什么我的工厂装载不正确?spec/factories 文件夹中的文件应该会自动加载。
回答by fkoessler
There seems to be an issue when using rspec / factory girl with spring.
将 rspec / factory girl 与 spring 一起使用时似乎存在问题。
Adding:
添加:
config.before(:all) do
FactoryGirl.reload
end
in my spec_helper.rb solved the issue.
在我的 spec_helper.rb 中解决了这个问题。
Credit: https://github.com/rails/spring/issues/88
信用:https: //github.com/rails/spring/issues/88
Edit:
编辑:
Another way to fix the issue is to manually tell Factory Girl where to load the factory. Add this in your spec_helper:
解决此问题的另一种方法是手动告诉 Factory Girl 在哪里加载工厂。将此添加到您的 spec_helper 中:
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
回答by David Moles
This seems to be an issue with Factory Bot. I fixed it (as per the issue report) with FactoryBot.find_definitions:
这似乎是Factory Bot 的问题。我修复了它(根据问题报告)FactoryBot.find_definitions:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before do
FactoryBot.find_definitions
end
end
回答by Obromios
This is not necessarily caused by Spring. There is an issuethat means factory_girl loads the paths slightly different from rspec. The solution is to to add to the rails_helper the following
这不一定是由 Spring 引起的。有一个问题意味着 factory_girl 加载的路径与 rspec 略有不同。解决方法是在 rails_helper 中添加以下内容
FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
FactoryGirl.find_definitions
assuming the helper is at engine_root/spec.
假设 helper 位于 engine_root/spec。
This occurs when you are using rspec in a rails engine.
当您在 rails 引擎中使用 rspec 时会发生这种情况。

