Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any override for rspec errors on BigDecimal?

Big Decimal's to_s prints as scientific notation.

rspec uses the to_s for error messages.

How could rspec be monkey-patched so that it prints errors for BigDecimal using a more conventional formatting?

This relates to this question: Override BigDecimal to_s default in Ruby

But I don't want to globally override BigDecimal.to_s.

like image 899
justingordon Avatar asked Dec 18 '25 20:12

justingordon


1 Answers

This works. Create file in rspec/support/big_decimal_inspect.rb

# Monkey patch BigDecimal#inspect
# Otherwises test errors print like:
# expected: #<BigDecimal:108482700,'0.11E3',9(27)>
#     got: #<BigDecimal:108468080,'0.12E3',9(27)>
# After get:
# expected: 110.0
#      got: 120.0
class BigDecimal
  def inspect
    to_s
  end
end
like image 84
justingordon Avatar answered Dec 21 '25 17:12

justingordon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!