2012-10-26 67 views
1

我是一個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 

感謝您的幫助。

+1

您是否爲用戶創建了工廠?像'FactoryGirl.define {factory:user ...}'。參考:https://github.com/thoughtbot/factory_girl/wiki/Usage –

+0

@shioyama,謝謝。我在工廠更新了這個問題。對不起,我離開了它。 – Jay

+0

@shioyama,我會嘗試創建一個沒有Faker數據的標準版本,看看它是否會通過。 – Jay

回答

2

從評論螺紋:你可能想在你的工廠改變factory :contactfactory :user

FactoryGirl.define do 
    factory :user do |f| 
    ... 
    end 
end 
1

試試這個,你app/spec/factories/文件夾中創建user.rb和寫

require 'factory_girl' 

FactoryGirl.define do 
    factory :user do 
    lname "test12" 
    fname "test12" 
    ... # whatever 
    end 
end 

,並在您user_spec.rb

describe User do 
    before { @user = FactoryGirl.create(:user, :fname => "abc", :lname => "fname") } 
    subject { @user } 
    it { should respond_to(:fname) } 
    it { should respond_to(:lname)} 
    it { should be_valid } 
end 
+0

+1獲得的見解。將給這個鏡頭,讓你知道它是如何工作的。 – Jay