rspec 仍然有点新,并且无法通过以下测试(问题区域“它“应该以正确的顺序进行正确的处理”块):
user_spec.rb
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
describe "treating associations" do
before { @user.save }
let!(:older_treating) do
FactoryGirl.create(:treating, user: @user, created_at: 1.day.ago)
end
let!(:newer_treating) do
FactoryGirl.create(:treating, user: @user, created_at: 1.hour.ago)
end
it "should have the right treatings in the right order" do
@user.sent_treatings.should == [newer_treating, older_treating]
@user.received_treatings.should == [newer_treating, older_treating]
end
end
end
根据下面的用户和处理模型,我知道我需要在测试的某个地方嵌入“请求者”和“被请求者”,并且我尝试了不同的变体,但它们都继续失败。以下是模型:
用户.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"
has_many :received_treatings, :foreign_key => "requestee_id", :class_name => "Treating"
end
治疗.rb
class Treating < ActiveRecord::Base
attr_accessible :intro, :proposed_date, :proposed_location
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
这是我的 factory.rb 文件:
工厂.rb
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"
user
end
end
寻找适当代码背后的逻辑解释以填写 user_spec 测试的“它“应该以正确的顺序进行正确的处理”块。谢谢!
编辑:对不起,忘记了错误信息,这里是:
失败:
1)用户处理关联应该以正确的顺序进行正确的处理失败/错误:FactoryGirl.create(:处理,用户:@user,created_at:1.day.ago)NoMethodError:未定义的方法user=' for #<Treating:0x0000010385ec70>
# ./spec/models/user_spec.rb:143:in
块(3级)在'