Fluent assertions in Elixir

Fluent assertions provide a live documentation of what the code actually does. Here's a small library I wrote in Elixir to make them.

Fluent assertions in Elixir

Good tests have a double purpose. The first one is to verify the code behaves as expected. The other one, not so obvious, is to provide a live documentation of what the code actually does.

Fluent assertions is a small step towards this second goal. We can help other programmers to understand the model faster, by writing tests with a language which is closer to the business.

ExMatchers is a small library I wrote in Elixir, which allows programmers to replace assertions such as:

test "map includes key with value" do
    map = %{:a => 1, 2 => :b, 'one' => 'two'}
    assert map[:a] == 1
end

with:

test "map includes key with value" do
    map = %{:a => 1, 2 => :b, 'one' => 'two'}
    expect map, to: include(:a),
              with: 1
end

Writing your own matchers

More exciting than the built-in matchers provided with the library is to write your own matchers specific to your domain model.

For example a billing system which generates invoices might have some tests such as:

test "invoice includes products" do
    invoice = BillingSystem.generate_invoice(client, products)
    expect invoice, to: include_product(iphone8),
                  with: Money.new(699.0, :USD)
end

test "invoice with promo code has a 10% discount" do
    invoice = BillingSystem.generate_invoice(client, products, promo_code)
    expect invoice, to: have_discount(10.0)
end

Enjoy!

6003255_keep_calm_and_start_testing-2