次のrspec検証エラーを解決できません。
Failures:
1) User treating associations should have the right treatings in the right order
Failure/Error: FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago)
ActiveRecord::RecordInvalid:
Validation failed: Requestee can't be blank
# ./spec/models/user_spec.rb:143:in `block (3 levels) in <top (required)>'
私は2つのモデル、ユーザーと治療(会議と考えてください)を扱っています:
user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :sent_treatings, :foreign_key => "requestor_id", :class_name => "Treating", dependent: :destroy
has_many :received_treatings, :foreign_key => "requestee_id", :class_name => "Treating", dependent: :destroy
end
治療.rb
class Treating < ActiveRecord::Base
attr_accessible :intro, :proposed_date, :proposed_location
validates :intro, presence: true, length: { maximum: 190 }
validates :requestor_id, presence: true
validates :requestee_id, presence: true
belongs_to :requestor, class_name: "User"
belongs_to :requestee, class_name: "User"
default_scope order: 'treatings.created_at DESC'
end
最後に、このエラーを発生させているuser_specの部分:
describe "treating associations" do
before { @user.save }
let!(:older_treating) do
FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago)
FactoryGirl.create(:treating, requestee: @user, created_at: 1.day.ago)
9end
let!(:newer_treating) do
FactoryGirl.create(:treating, requestor: @user, created_at: 1.hour.ago)
FactoryGirl.create(:treating, requestee: @user, created_at: 1.hour.ago)
end
it "should have the right treatings in the right order" do
@sent_treating = requestor.sent_treatings.build(intro: "Lorem ipsum")
@sent_treating.requestee = requestee
@received_treating = requestee.received_treatings.build(intro: "Lorem ipsum")
@received_treating.requestor = requestor
requestor.sent_treatings.should == [newer_treating, older_treating]
requestee.received_treatings.should == [newer_treating, older_treating]
end
'requestee'がここで空白になり、ヘルプを使用できる理由がわかりません。ありがとうございました。
編集:工場は下にあります、ありがとう!
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
factory :treating do
intro "Lorem ipsum"
end
end
(:treatingファクトリの「intro」の下に「user」、「requestor」、および「requestee」を追加しようとしましたが、それでも成功しません)。ファクトリファイル(イントロ「Loremipsum」の下)に「user」を追加すると、次のエラーが発生します。
1) User treating associations should have the right treatings in the right order
Failure/Error: FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago)
NoMethodError:
undefined method `user=' for #<Treating:0x00000104fa0668>
# ./spec/models/user_spec.rb:143:in `block (3 levels) in <top (required)>'
ユーザーの代わりに(そして使用に加えて)ファクトリに「requestor」と「requestee」を追加すると、次のエラーが発生します。
Failures:
1) User treating associations should have the right treatings in the right order
Failure/Error: FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago)
ArgumentError:
Trait not registered: requestee