问题描述
我正在尝试让工厂女孩在我的Rails 4.1.1应用程序中使用rspec运行.
I am trying to get factory girl to run with rspec in my rails 4.1.1 app.
问题是当我在命令行中运行rspec
时,我得到了Failure/Error: verse = build(:verse) ArgumentError: Factory not registered: verse
.
Problem is when I run rspec
in my command line, i get 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.
在我的Gemfile中:
in my Gemfile:
gem 'rails', '4.1.1'
group :development, :test do
gem 'rspec-rails'
gem "factory_girl_rails"
end
我的spec_helper.rb文件:
my spec_helper.rb file:
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec/controllers/api/verses_controller_spec.rb
spec/controllers/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
spec/factories/verses.rb
FactoryGirl.define do
factory :verse do
line1 "A beautiful verse I stand"
end
end
为什么我的工厂无法正常加载? spec/factories文件夹中的文件应该会自动加载.
Why isn't my factory loading properly? Files in the spec/factories folder are supposed to get loaded automatically.
推荐答案
在弹簧上使用rspec/factory girl时似乎存在问题.
There seems to be an issue when using rspec / factory girl with spring.
添加:
config.before(:all) do
FactoryGirl.reload
end
在我的spec_helper.rb中解决了该问题.
in my spec_helper.rb solved the issue.
信用: https://github.com/rails/spring/issues/88
解决此问题的另一种方法是手动告诉Factory Girl在哪里加载工厂.将此添加到您的spec_helper中:
Another way to fix the issue is to manually tell Factory Girl where to load the factory. Add this in your spec_helper:
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
这篇关于ArgumentError:工厂未注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!