0

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.

markain
  • 371
  • 5
  • 15
  • Please check this answer http://stackoverflow.com/questions/24682671/how-to-complete-the-rspec-put-controller-test-from-scaffold/24739399#24739399 for a painless general way to check object equality (Date, BigDecimal etc...) – Benjamin Bouchet Aug 03 '14 at 00:58
  • You could write expects for only the most important attributes. For example, check the `created_at`, `amount`, etc. – Lucas Nogueira Aug 03 '14 at 01:07
  • thanks for both. both suggestions are similar in nature. Benjamin provided a good example. for anyone looking: control f to "assert_record_values is the helper that will make your rspec simpler." The poster from Benjamin's link also has a nice summary of his solution down at the bottom of his thread. – markain Aug 03 '14 at 15:51

0 Answers0