我无法解决以下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)>'
我正在处理两种模式,即用户和治疗(将其视为会议):
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
treating.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
我不确定为什么'被请求者'会在这里空白,可以使用一些帮助。谢谢。
编辑:工厂在下面,谢谢!
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
(我试图在:处理工厂中的'intro'下面添加'user','requestor'和'requestee',但仍然没有成功)。当我在工厂文件中添加'user'时(在'intro“Lorem ipsum”下面),我收到以下错误:
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
答案 0 :(得分:1)
问题是您的治疗工厂没有定义请求者和被请求者,此外,您需要告诉FactoryGirl请求者和被请求者是用户的别名
factory :user, :aliases => [:requestor, :requestee] 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"
requestor
requestee
end
答案 1 :(得分:1)
这里是修复。
factories.rb需要在:user trait line上提到的别名为
FactoryGirl.define do
factory :user, aliases: [:requestor, :requestee] 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"
requestor
requestee
end
end
最后,&#34;治疗协会&#34;测试需要将旧的/更新的接收/发送处理(4种变体)分解为:
describe "treating associations" do
before { @user.save }
let!(:older_sent_treating) do
FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago)
end
let!(:older_received_treating) do
FactoryGirl.create(:treating, requestee: @user, created_at: 1.day.ago)
end
let!(:newer_sent_treating) do
FactoryGirl.create(:treating, requestor: @user, created_at: 1.hour.ago)
end
let!(:newer_received_treating) do
FactoryGirl.create(:treating, requestee: @user, created_at: 1.hour.ago)
end
it "should have the right treatings in the right order" do
@user.sent_treatings.should == [newer_sent_treating, older_sent_treating]
@user.received_treatings.should == [newer_received_treating, older_received_treating]
end
end
那么测试应该通过。谢谢!