2013-07-29 35 views
1

所以我有一個奇怪的問題,我有3個工廠宣佈,其中只有1個工廠初始化罰款,其餘的人給予了非恆定不變。 我不知道我在做什麼錯。 我知道這個問題已被問過很多次,但沒有一個答案解決我的問題。工廠女孩:NameError unintialized常數爲一個工廠

這裏是我的規格/工廠/ sample_factory.rb

require 'faker' 

FactoryGirl.define do 

factory :early_access do 
    email {Faker::Internet.email} 
end 

factory :login do 
    email {Faker::Internet.email} 
    password "password" 
end 

factory :user do 
     display_name {Faker::Internet.name} 
     email {Faker::Internet.email} 
     username {Faker::Internet.username} 
     password "password" 
end 

這裏是型號規格文件規範/模型/ sample.rb

require 'spec_helper.rb' 

describe EarlyAccess do 

    it "has a valid factory" do 
     FactoryGirl.build(:early_access).should be_valid 
    end 

    it "has a valid factory login" do 
     FactoryGirl.build(:login).should be_valid 
    end 
end 


    it "has valid factory user" do 
     FactoryGirl.build(:user).should be_valid 
    end 
end 

這些中,只有第一個early_access通行證其餘的失敗

.FF 

Failures: 

    1) EarlyAccess has a valid factory 2 
    Failure/Error: FactoryGirl.build(:login).should be_valid 
    NameError: 
     uninitialized constant Login 
    # ./spec/models/sample.rb:10:in `block (2 levels) in <top (required)>' 

    2) EarlyAccess has valid factory user 
    Failure/Error: 
    NameError: 
     uninitialized constant User 
    # ./spec/models/sample.rb:16:in `block (2 levels) in <top (required)>' 

我已經在我的spec_helpe中包含了factory_girl_rails河

+2

有型號登錄和用戶?它試圖根據工廠名稱初始化模型。如果工廠名稱是用戶,它將嘗試執行'User.new'。 – usha

+0

我不認爲它與您的問題有關,但是您的規格列表中​​存在語法錯誤。第三個'it'調用不被'describe'包圍。 –

回答

2
  1. 請確保您有機型EarlyAccess,登錄,用戶自定義
  2. 的車型應手動事先要求或符合的ActiveSupport ::依賴自動加載約定(如位於應用程序/模型/ login.rb應用程序/ models/user.rb)。

如果您有這些模型位於某個非標準目錄 - 需要它們手動或將目錄添加到config/application.rb config.autoload_paths數組。