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)

注文とユーザーが作成されます...

Rubymanが提案したように更新2 、私はいくつかのことを変更しました:

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

tdgs からのアドバイスを読んだ後のupdate 3ここに変更があります: モデルに変更はありません:

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 N は最初に書いておくべきでした。コンソールで実行しました(この問題が解決することを願っています)

bundle exec rake db:migrate
bundle exec rake db:migrate:reset db:test:prepare
4

4 に答える 4

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

次に、テストは次のように作成する必要があります。

 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)

于 2012-10-12T10:06:40.417 に答える
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
于 2012-10-15T14:35:37.807 に答える