I am comparing two address objects in an RSpec test, but the test fails even though the attributes appear to be having the same values. I am using expect(object1).to eq(object2)
and receive the following output:
Failures:
1) Property#address= sets all address fields
Failure/Error: expect(property).to eq(property_address)
expected: #<Property id: nil, property_type_id: nil, street: "780 Monterey Blvd", unit: "301", city: "San Francisco", state: "CA", zip: "94127", country: "US", square_feet: "906", beds: "2", baths: "1", floors: "1", created_at: nil, updated_at: nil, user_id: 348>
got: #<Property id: nil, property_type_id: nil, street: "780 Monterey Blvd", unit: "301", city: "San Francisco", state: "CA", zip: "94127", country: "US", square_feet: "906", beds: "2", baths: "1", floors: "1", created_at: nil, updated_at: nil, user_id: 348>
(compared using ==)
Diff:
Using pry-rescue
to look at the actual attributes and compare them, I receive the following:
[3] pry(RSpec::Expectations)> actual.attributes
=> {"id"=>nil,
"property_type_id"=>nil,
"street"=>"780 Monterey Blvd",
"unit"=>"301",
"city"=>"San Francisco",
"state"=>"CA",
"zip"=>"94127",
"country"=>"US",
"square_feet"=>"906",
"beds"=>"2",
"baths"=>"1",
"floors"=>"1",
"created_at"=>nil,
"updated_at"=>nil,
"user_id"=>335}
[4] pry(RSpec::Expectations)> expected.attributes
=> {"id"=>nil,
"property_type_id"=>nil,
"street"=>"780 Monterey Blvd",
"unit"=>"301",
"city"=>"San Francisco",
"state"=>"CA",
"zip"=>"94127",
"country"=>"US",
"square_feet"=>"906",
"beds"=>"2",
"baths"=>"1",
"floors"=>"1",
"created_at"=>nil,
"updated_at"=>nil,
"user_id"=>335}
[5] pry(RSpec::Expectations)> differ.diff(actual.attributes, expected.attributes)
=> "\e[0m\n\e[0m"
I can't spot any difference. Where is the diff
-difference coming from?
SOLUTION
What through me off and why I hadn't followed the answer in How to test for (ActiveRecord) object equality, was the fact that while in pry-rescue
, the output of
differ.diff(actual.attributes, expected.attributes)
showed the same difference as the actual failed spec. However, following the answer for How to test for (ActiveRecord) object equality I gave it a shot and changed the actual spec to use expect(property.attributes).to eq(property_address.attributes)
and now the spec passes.