Assertions v0.6.0 Assertions View Source

Helpful functions to help you write better tests.

Link to this section Summary

Functions

Asserts that two lists are equal without asserting they are in the same order

Asserts that two lists are equal without asserting they are in the same order, and outputs the given failure message

Asserts that a map with the same values for the given keys is in the list

Asserts that the values in map left and map right are the same for the given keys

Asserts that a struct with the same values for the given keys is in the list

Asserts that the values in map left and map right are the same for the given keys

Link to this section Functions

Link to this macro assert_lists_equal(left, right) View Source (macro)

Asserts that two lists are equal without asserting they are in the same order.

iex> assert_lists_equal([1,2,3], [1,3,2])
true

iex> try do
iex>   assert_lists_equal([1,2,4], [1,3,2])
iex> rescue
iex>   error in [ExUnit.AssertionError] ->
iex>     assert error.message == "Comparison of each element failed!"
iex> end
true
Link to this macro assert_lists_equal(left, right, message) View Source (macro)

Asserts that two lists are equal without asserting they are in the same order, and outputs the given failure message.

iex> assert_lists_equal([1,2,3], [1,3,2])
true

If you want, you can provide a custom failure message.

iex> try do
iex>   assert_lists_equal([1,2,4], [1,3,2], "NOT A MATCH")
iex> rescue
iex>   error in [ExUnit.AssertionError] ->
iex>     assert error.message == "NOT A MATCH"
iex> end
true

You can also provide a custom function to use to compare your elements in your lists.

iex> assert_lists_equal(["dog"], ["cat"], &(String.length(&1) == String.length(&2)))
true

You can also provide a custom failure message as well as a custom function to use to compare elements in your lists.

iex> try do
iex>   assert_lists_equal(["dog"], ["lion"], &(String.length(&1) == String.length(&2)), "FAILED WITH CUSTOM MESSAGE")
iex> rescue
iex>   error in [ExUnit.AssertionError] ->
iex>     assert error.message == "FAILED WITH CUSTOM MESSAGE"
iex> end
true
Link to this macro assert_lists_equal(left, right, comparison, message) View Source (macro)
Link to this macro assert_map_in_list(map, list, keys) View Source (macro)

Asserts that a map with the same values for the given keys is in the list.

iex> list = [%{first: :first, second: :second, third: :third}]
iex> assert_map_in_list(%{first: :first, second: :second}, list, [:first, :second])
true
Link to this macro assert_maps_equal(left, right, keys) View Source (macro)

Asserts that the values in map left and map right are the same for the given keys

iex> left = %{first: :first, second: :second, third: :third}
iex> right = %{first: :first, second: :second, third: :fourth}
iex> assert_maps_equal(left, right, [:first, :second])
true
Link to this macro assert_struct_in_list(struct, list, keys) View Source (macro)

Asserts that a struct with the same values for the given keys is in the list.

iex> list = [DateTime.utc_now(), Date.utc_today()]
iex> assert_struct_in_list(DateTime.utc_now(), list, [:year, :month, :day, :second])
true
Link to this macro assert_structs_equal(left, right, keys) View Source (macro)

Asserts that the values in map left and map right are the same for the given keys

iex> assert_structs_equal(DateTime.utc_now(), DateTime.utc_now(), [:year, :minute])
true