I’m having trouble getting my test to pass. In my application, a User can have many Meetings that they request (sent_meetings) and those that are requested of them (received_meetings).
First, my (simplified) user.rb model:
user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :sent_meetings, :foreign_key => "requestor_id", :class_name => "Meeting"
has_many :received_meetings, :foreign_key => "requestee_id", :class_name => "Meeting"
end
And my meeting.rb model:
meeting.rb
class Meeting < ActiveRecord::Base
attr_accessible :intro, :proposed_date, :proposed_location
belongs_to :requestor, class_name: "User"
belongs_to :requestee, class_name: "User"
end
And my test:
meeting_spec.rb
require 'spec_helper'
describe Meeting do
let(:requestee) { FactoryGirl.create(:user) }
let(:requestor) { FactoryGirl.create(:user) }
before { @received_meeting = requestee.received_meetings.build(intro: "Lorem ipsum") }
before { @sent_meeting = requestor.sent_meetings.build(intro: "Lorem ipsum") }
subject { @sent_meeting }
it { should respond_to(:intro) }
it { should respond_to(:requestor_id) }
it { should respond_to(:requestor) }
its(:requestor) { should == requestor }
# it { should be_valid }
subject { @received_meeting }
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
There seems to be a conflict between the two “subject” lines in my spec file (@sent_meeting and @received_meeting), and that one is overriding the other. Here is my failed test message:
Failures:
1) Meeting requestor
Failure/Error: its(:requestor) { should == requestor }
expected: #
got: nil (using ==)
# ./spec/models/meeting_spec.rb:15:in `block (2 levels) in ‘
Finished in 0.67458 seconds
36 examples, 1 failure
Failed examples:
rspec ./spec/models/meeting_spec.rb:15 # Meeting requestor
I found it interesting that I was getting this error only for either requestor OR requestee (obviously there is a conflict). The ordering of the two subject code chunks matters, and when I do switch them, I get the same error as above but in regard to ‘requestee’ instead of ‘requestor’.
How can I get the test to pass? Thank you!
You cant do this. You must do this :
You can also use let instead before like this :
It will be faster.