Elixir Money - Assert Money.equals?(lhs, rhs) Verbosity


When writing ExUnit tests and performing assert with ==, if the sides do not match it prints out something pretty like this where the sides are passed as part of the console output.

Test

test "find equality function" do
  assert 5 == 6
end

Output

1) test find equality function (MyAppTest)
     test/my_app_test.exs:341
     Assertion with == failed
     code:  5 == 6
     left:  5
     right: 6
     stacktrace:
       test/my_app_test.exs:350: (test)

If we use the Money Library, with the Money.equals?(lhs, rhs) function it just prints out the boolean return as expect as that is what the function should return. 

Test


test "find Money equality function" do
  lhs = Money.new(100, :USD)
  rhs = Money.new(101, :USD)
  assert Money.equals?(lhs, rhs)
end

 

Output

2) test find Money equality function (MyAppTest)
     test/my_app_test.exs:355
     Expected truthy, got false
     code: Money.equals?(lhs, rhs)
     stacktrace:
       test/my_app_test.exs:358: (test)          

 

We can get a more verbose output of the equality of Money with the `==` operator

Test

test "find Money equality function" do
  lhs = Money.new(100, :USD)
  rhs = Money.new(101, :USD)
  assert lhs == rhs
end

Output

1) test find Money equality function (MyRewardsTest)
     test/my_rewards_test.exs:351
     Assertion with == failed
     code:  lhs == rhs
     left:  %Money{currency: :USD, amount: 100}
     right: %Money{currency: :USD, amount: 101}
     stacktrace:
       test/my_rewards_test.exs:354: (test)


Home Office

Cherry Hill, NJ

North New Jersey Office

Hasbrouck Heights, NJ

Philadelphia Office

Media, PA

office Hours

Mon - Fri : 8am to 6pm

Social Media