2012-10-12 43 views
1

美好的一天,我從早該工廠女孩錯誤找不到模型

ActiveRecord::RecordNotFound: 
    Couldn't find User without an ID 

我的模型得到這個錯誤

has_many :objects, class_name: 'OrderObject', dependent: :destroy 
    belongs_to :user 
    belongs_to :tariff 

    validates :client, :phone, :tariff_id, :days, :user_id, presence: true 

規範

before do 
    user = FactoryGirl.create(:user) 
    FactoryGirl.create(:order, user_id: user.id) 
    end 
     context "validations" do 
     it { should validate_presence_of :client } 
     it { should validate_presence_of :phone } 
     it { should validate_presence_of :tariff_id } 
     it { should validate_presence_of :days } 
     end 

     it { should have_many(:objects) } 
     it { should belong_to(:tariff) } 
     it { should belong_to(:user) } 

factory :order do 
    client "MyString" 
    phone "MyString" 
    tariff_id 1 
    days 1 
    # advt_payed_day 1 
    # firm_payed_day 1 
    user_id 1 
    end 

更新1

changed to 
    before(:all) do 
    user = FactoryGirl.create(:user) 
    puts user.id 
    order = FactoryGirl.create(:order, user_id: user.id) 
    puts order.id 

    end 

輸出

Order 
45 
32 
    should have many objects 
    should belong to tariff 
    should belong to user 
    validations 
    should require client to be set (FAILED - 1) 
    should require phone to be set (FAILED - 2) 
    should require tariff_id to be set (FAILED - 3) 
    should require days to be set (FAILED - 4) 
    should require user_id to be set (FAILED - 5) 

所以下令&用戶創建...

更新2 爲Rubyman建議,我已經改變了的夫婦東西:

in spec 
    before(:all) do 
    user = FactoryGirl.create(:user) 
    #puts user.id 
    order = FactoryGirl.create(:order, user_id: user.id) 
    puts order 
    puts order.user_id 

    end 
在工廠

factory :order do 
    client "MyString" 
    phone "MyString" 
    tariff_id 1 
    days 1 
    # user_id 1 
    association :user, factory: :user 
    end 

輸出

是:

Order 
#<Order:0x00000005a866a0> 
46 
    should have many objects 
    should belong to tariff 
    should belong to user 
    validations 
    should require client to be set (FAILED - 1) 
    should require phone to be set (FAILED - 2) 
    should require tariff_id to be set (FAILED - 3) 
    should require days to be set (FAILED - 4) 
    should require user_id to be set (FAILED - 5) 

1) Order validations 
    Failure/Error: it { should validate_presence_of :client } 
    ActiveRecord::RecordNotFound: 
     Couldn't find User without an ID 
    # ./app/models/order.rb:35:in `user_is_not_admin?' 
    # ./spec/models/order_spec.rb:14:in `block (3 levels) in <top (required)>' 

更新3TDGS閱讀意見後這裏的變化: 在模型中沒有變化:

validates :client, :phone, :tariff_id, :days, :user_id, presence: true 

在規範

describe Order do 
    before(:each) do 
    user = FactoryGirl.create(:user) 
    #puts user.id 
    order = FactoryGirl.create(:order, user_id: user.id) 
    puts order 
    puts order.user_id 
    puts order.tariff_id 
    puts order.phone 
    puts order.days 
    puts order.client 
    puts '*****' 
    user = User.find(order.user_id) 
    puts user.login 

    end 
    context "validations" do 
    it { should validate_presence_of :client } 
    it { should validate_presence_of :phone } 
    it { should validate_presence_of :tariff_id } 
    it { should validate_presence_of :days } 
    it { should validate_presence_of :user_id } 
    end 

    it { should have_many(:objects) } 
    it { should belong_to(:tariff) } 
    it { should belong_to(:user) } 
end 

輸出:

#<Order:0x00000006c10ce0> 
161 
101 
MyString 
1 
MyString 
***** 
user__7 
    should require days to be set (FAILED - 1) 

輸出每應該是有效的,據我看到...

UPDATEñ 應在開始寫它。我已經運行(希望它會解決這個問題)的控制檯

bundle exec rake db:migrate 
bundle exec rake db:migrate:reset db:test:prepare 
+0

我也找不到一個沒有ID的模型,不得不解決一個工廠的女孩。也許有一天,當我變得富有和出名的時候。 –

+0

自開始以來更改了問題) – Elmor

+1

shoulda測試不使用您在之前的過濾器中創建的對象。嘗試像這個主題設置主題{FactoryGirl.create:order} – tdgs

回答

1

首先,您的工廠定義不是defini ng關聯正確。你應該有這樣的事情:

FactoryGirl.define do 
    factory :user do 
    sequence(:username) {|n| "username_#{n}"} 
    # more attributes here 
    end 

    factory :tariff do 
    # attributes 
    end 

    factory :order do 
    client "MyString" 
    phone "MyString" 
    tariff 
    user 
    days 1 
    end 
end 

那麼你的測試應該這樣寫:

context "validations" do 
    it { should validate_presence_of :client } 
    it { should validate_presence_of :phone } 
    it { should validate_presence_of :tariff_id } 
    it { should validate_presence_of :days } 
    end 

    it { should have_many(:objects) } 
    it { should belong_to(:tariff) } 
    it { should belong_to(:user) } 

所有您目前在before過濾器的代碼,現在是不相關的。另外請注意,在運行測試時使用before(:all)可能會產生一些奇怪的效果,因爲它們不在交易中運行。另一方面,before(:each)呢。

0

試試這個

before { 
    @user = FactoryGirl.create(:user, :email => "test.com", :password => "test123", ...) 
    @order = FactoryGirl.create(:order, :user_id => @user.id) 
    } 

require 'factory_girl' 
FactoryGirl.define do 
factory :order do 
    client "MyString" 
    ... 
    ... 
end 
end 
0

我離開了shoulda並重寫了驗證檢查。這樣它的工作原理:

before(:each) do 
    @user = FactoryGirl.create(:user) 
    @order = FactoryGirl.create(:order, user_id: @user.id) 

    end 
    it 'absence of client isn\'t acceptable' do 
     temp = @order.client 
     @order.client = '' 
     @order.should_not be_valid 
     @order.client = temp 
     @order.should be_valid 
    end