좋은 하루의 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
ID없는 모델을 찾을 수 없으며 공장 출신의 소녀에게 정착해야했습니다. 어쩌면 언젠가는 내가 부자가되고 유명해질지도 모른다. –
처음부터 문제가 변경됨) – Elmor
shoulda 테스트는 이전 필터에서 만든 개체를 사용하지 않습니다. 이 주제와 같은 주제를 설정해보십시오. {FactoryGirl.create : order} – tdgs