我是一個factory_girl noob,並且無法確定工廠爲什麼沒有「註冊」。不知道它在哪裏註冊。的Gemfile的factory_girl_rails 4.1.0 in rails 3.2環境生產'工廠未註冊:用戶'
相關部分:
...
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
end
group :test do
gem 'faker'
gem 'capybara'
gem 'guard-rspec'
gem 'launchy'
end
...
配置/ application.rb中的相關部分:
...
module Appname
class Application < Rails::Application
...
config.generators do |g|
g.test_framework :rspec,
:fixtures => true,
:view_specs => false,
:helper_specs => false,
:routing_specs => true,
:controller_specs => true,
:request_specs => true
g.fixture_replacement :factory_girl, :dir => "spec/factories"
end
...
end
end
規格/模型/ user_spec.rb的相關部分:
require 'spec_helper'
describe User do
it "has a valid factory" do
FactoryGirl.create(:user).should be_valid
end
...
end
結果$ rspec spec/models/user_spec.rb:
Failures:
1) User has a valid factory
Failure/Error: FactoryGirl.create(:user).should be_valid
ArgumentError:
Factory not registered: user
UPDATE:
規格/工廠/ users.rb的:
require 'faker'
FactoryGirl.define do
factory :contact do |f|
f.first_name { Faker::Name.first_name }
f.last_name { Faker::Name.last_name }
f.email { Faker::Internet.email }
f.password { Faker::Name.last_name }
f.password_confirmation { Faker::Name.last_name }
end
end
感謝您的幫助。
您是否爲用戶創建了工廠?像'FactoryGirl.define {factory:user ...}'。參考:https://github.com/thoughtbot/factory_girl/wiki/Usage –
@shioyama,謝謝。我在工廠更新了這個問題。對不起,我離開了它。 – Jay
@shioyama,我會嘗試創建一個沒有Faker數據的標準版本,看看它是否會通過。 – Jay