以下测试均通过除了了“它{应be_valid}”行“描述‘发送treatings’做”和“描述‘收到treatings’做”规范验证失败
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
任何帮助表示赞赏!
什么是你测试的对象吗?在你的代码中进行Meating或Treating?你可以发布它的来源吗? – bento 2012-08-01 05:05:15