Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

How to assert Elixir doctest raises an error

Today I learned how to assert an Elixir doctest raises an error. Check this out:

defmodule MyModule do
  @doc """
  This function raises ArgumentError.

  ## Examples

      iex> MyModule.my_func()
      ** (ArgumentError) something is wrong
  def my_func() do
    raise(ArgumentError, "something is really wrong")

The previous doctest will fail with this message:

  1) doctest MyModule.my_func/0 (1) (MyModuleTest)
     Doctest failed: wrong message for ArgumentError
       "something is wrong"
       "something is really wrong"
     code: MyModule.my_func()
       lib/my_module.ex:10: MyModule (module)
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.