RubyMoney / money

A Ruby Library for dealing with money and currency conversion.
http://rubymoney.github.io/money
MIT License
2.75k stars 626 forks source link

RSpec have_attributes matcher and zero numerics #1010

Closed katelovescode closed 2 years ago

katelovescode commented 2 years ago

Right now I have a model and a spec and I can only do the matcher specifically with eq as opposed to have_attributes, but only when the number is not zero.

ex:

class Model

def fee
  Money.from_amount(super)
end

#  fee           :decimal(, )      not null

This column was created before we started using Money, so we're converting it in the getter

expect(model).to have_attributes({ fee: 0 }) # passes when the value is zero
expect(model).to have_attributes({ fee: 120 }) # fails when the value is not zero with Money#== supports only zero numerics
expect(model).to have_attributes({ fee: Money.from_amount(120) }) # fails with Money#== supports only zero numerics
expect(model.fee).to eq(Money.from_amount(120)) # passes
expect(model.fee).to eq(120) # fails with Money#== supports only zero numerics

Is there a reason the fourth example passes but the third example fails with the have_attributes matcher?

deepakhb2 commented 2 years ago

I think the have_attributes method directly pull the values that is stored in table so it will not be mapped to Money class object and also the value of field fee is not stored as object of Money class (ref: link). But the 'model.fee' will return an object of money class this this is done by money gem. I hope this answers your question. Thank you.