Rails 4中的“Factory not registered”错误 [英] 'Factory not registered' error in Rails 4
问题描述
我在Rails 4中使用Factory Girl进行了Factory not registered错误。这是我在factory / user.rb中的简单Devise用户的工厂定义:
I am getting a 'Factory not registered' error using Factory Girl in Rails 4. Here is my factory definition for a simple Devise user in spec/factories/user.rb:
FactoryGirl.define do
factory :user do
email 'test@example.com'
password 'foobar'
password_confirmation 'foobar'
end
end
这是spec_helper.rb: / p>
Here is the spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'email_spec'
require 'rspec/autorun'
require 'factory_girl'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Devise::TestHelpers, :type => :controller
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
在宝石文件中:
gem 'factory_girl_rails', :require => false
这里是spec / models / user_spec.rb中的测试:
And here is the test in spec/models/user_spec.rb:
require 'spec_helper'
describe User do
it "has a valid factory" do
FactoryGirl.build(:user).should be_valid
end
end
说工厂未注册:用户。我已经尝试将Factory定义移动到spec / factoryories.rb,以及只使用factory_girl而不是factory_girl_rails,没有运气。任何建议将不胜感激。
This fails, saying 'Factory not registered: user'. I've tried moving the Factory definition to spec/factories.rb, as well as using just factory_girl instead of factory_girl_rails, with no luck. Any suggestions would be greatly appreciated.
推荐答案
您需要 factory_girl
而不是 factory_girl_rails
。将您的 spec_helper.rb
中的等效行编辑为:
You're requiring factory_girl
instead of factory_girl_rails
. Edit the equivalent line in your spec_helper.rb
to this:
require 'factory_girl_rails'
这篇关于Rails 4中的“Factory not registered”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!