以下测试都通过了,除了“描述“发送的处理”做和“描述“接收的处理”做'中的“it { should be_valid}”行
require 'spec_helper'
describe Treating do
let(:requestee) { FactoryGirl.create(:user) }
let(:requestor) { FactoryGirl.create(:user) }
before { @received_treating = requestee.received_treatings.build(intro: "Lorem ipsum") }
before { @sent_treating = requestor.sent_treatings.build(intro: "Lorem ipsum") }
describe "sent treatings" do
subject { @sent_treating }
it { should respond_to(:intro) }
it { should respond_to(:requestor_id) }
it { should respond_to(:requestor) }
its(:requestor) { should == requestor }
it { should be_valid }
end
describe "received treatings" do
subject { @received_treating }
it { should respond_to(:intro) }
it { should respond_to(:requestee_id) }
it { should respond_to(:requestee) }
its(:requestee) { should == requestee }
it { should be_valid }
end
describe "accessible attributes" do
it "should not allow access to requestor_id" do
expect do
Treating.new(requestor_id: requestor.id)
end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
it "should not allow access to requestee_id" do
expect do
Treating.new(requestee_id: requestee.id)
end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
describe "when requestor_id is not present" do
before { @sent_treating.requestor_id = nil }
it { should_not be_valid }
end
describe "when requestee_id is not present" do
before { @received_treating.requestee_id = nil }
it { should_not be_valid }
end
end
这是错误:
Failures:
1) Treating sent treatings
Failure/Error: it { should be_valid }
expected valid? to return true, got false
# ./spec/models/treating_spec.rb:19:in `block (3 levels) in <top (required)>'
2) Treating received treatings
Failure/Error: it { should be_valid }
expected valid? to return true, got false
# ./spec/models/treating_spec.rb:30:in `block (3 levels) in <top (required)>'
最后,我的 user.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"
end
任何帮助表示赞赏!