RubyMoney/money

RSpec have_attributes matcher and zero numerics

Closed this issue · 1 comments

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?

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.