The following tests are all passing except the “it { should be_valid }” lines in ‘describe “sent treatings” do’ and ‘describe “received treatings” do’
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
here is the error:
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)>'
lastly, my user.rb model:
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
any help is appreciated!
In this test you have two meetings and set only one of the two ids of each of them in the fixture:
assuming user is the same as in your last question,
@received_treating should have an requestee_id but not an requester_id (has never been assigned anywhere!) and @sent_treating has a requestor_id.
so again, for the same reasons as in your last question, the validation fails, as both have only one of the two ids requested in the validation set.
What is the behaviour that you expect? If you want to build up the n:m relationship to the users, you will have to specify the second user at some point. Maybe you mean a fixture like that:
maybe you even want to create a custom setter in Treating
then you could write something like
That gives you a Treating with both ids set.