2012-10-12 9 views
1

좋은 하루의 ID없이 모델을 찾을 수 없습니다했어야 공장 소녀 오류가 난</p> <pre><code>ActiveRecord::RecordNotFound: Couldn't find User without an ID </code></pre> <p>내 모델이 오류,

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 
Rubyman 제안이

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) 

그렇게 & 사용자를 주문 출력이 생성됩니다 UPDATE 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 

...

업데이트, 나는 몇을 변경했습니다 사물 :

공장

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

및 출력 63,210

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 

은 다음과 같습니다 tdgs 조언을 읽은 후

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)>' 

업데이트 3 여기 변경됩니다 모델의 변경없이 :

validates :client, :phone, :tariff_id, :days, :user_id, presence: true 
사양에 53,691,363,210

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 N 처음에이를 기록해야

... 최대한 멀리 참조로서 유효하다. 내가 실행 한 공장 소녀와 연결을 만드는 방법을

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

ID없는 모델을 찾을 수 없으며 공장 출신의 소녀에게 정착해야했습니다. 어쩌면 언젠가는 내가 부자가되고 유명해질지도 모른다. –

+0

처음부터 문제가 변경됨) – Elmor

+1

shoulda 테스트는 이전 필터에서 만든 개체를 사용하지 않습니다. 이 주제와 같은 주제를 설정해보십시오. {FactoryGirl.create : order} – tdgs

답변

1

먼저 공장 정의가 정의되지 않았습니다. 연결을 올바르게 설정하십시오.

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 

이 그런 다음 테스트는 다음과 같이 기록한다 : 당신이 현재 before 필터가

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(:all)을 사용하면 트랜잭션 내부에서 실행되지 않으므로 테스트를 실행할 때 이상한 영향을 미칠 수 있습니다. 반면에 before(:each).

0

확인이

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

공장을 시도 콘솔

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

업데이트 2를 완료했습니다.) – Elmor

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