I'm having trouble running a unit test to check if a decimal
value is what I've expected. This is what I've tried:
func Test_example(t *testing.T) {
t.Run("test if two decimals are equal", func(t *testing.T) {
sum_amount := decimal.NewFromFloat(1000.00)
.Add(decimal.NewFromFloat(5000.00))
require.Equal(t, decimal.NewFromFloat32(6000.00), sum_amount))
})
}
As you can see the sum_amount
is a decimal
. However comparing the two in the test case work out slightly differently with exponents etc.
How do I properly assert that these values are equal with stretchr/testify?
E.g. here's the diff:
Diff:
--- Expected
+++ Actual
@@ -4,6 +4,6 @@
abs: (big.nat) (len=1) {
- (big.Word) 6
+ (big.Word) 6000
}
}),
- exp: (int32) 3
+ exp: (int32) 0
}
An alternative to decimal.Decimal.Equal
that may work for most cases and still yields an informative error message when the assertion fails - is comparing the string representations of the expected and asserted decimals:
require.Equal(t, decimal.NewFromFloat(6000.00).String(), sum_amount.String())