I'm creating an active record object, and I want to test that I'm doing it correctly.
Here's my test code:
describe '#transaction process' do
let(:penalty_purchase) { penalty.penalty_purchase }
describe '#transaction' do
it 'a transaction between the seller and the admin, includes the penalty purchase and the sellers sale' do
penalty.purchase= penalty_purchase
args = {sale: sale, purchase: penalty_purchase}
wanted = penalty.transaction_factory.new(args).transaction
expect(penalty.transaction).to eql wanted
end
end
end
Heres the code my test is testing:
def penalty_transaction
args = {sale: sale, purchase: purchase}
@transaction = transaction_factory.new(args).transaction
end
The transaction factory object above takes in 2 other active record objects, a sale and a purchase.
For some reason the test is failing with this:
1) LockedSalePenalty purchase process #transaction process #transaction a transaction between the seller and the admin, includes the penalty purchase and the sellers sale
Failure/Error: expect(penalty.transaction).to eql wanted
expected: #<Transaction id: nil, purchase_id: nil, sale_id: 1, created_at: "2014-08-03 00:23:45", updated_at: nil, seller_id: 1, buyer_id: 2, amount: 20.0, reduced_asset_id: nil>
got: #<Transaction id: nil, purchase_id: nil, sale_id: 1, created_at: "2014-08-03 00:23:45", updated_at: nil, seller_id: 1, buyer_id: 2, amount: 20.0, reduced_asset_id: nil>
(compared using eql?)
I know that for objects you create you need implement your own equality methods for objects you create, so in my active record transaction class:
def eql?(obj)
self.class == obj.class &&
self.attributes == obj.attributes
end
When I compare the attributes of penalty.transaction and wanted, they are not equal, even though they look the exact same. Any ideas?
Edit:
The problem was that the created at attribute was different by a small amount.