View Source Tracee (Tracee v0.2.0)

This Elixir library offers functionality to trace and assert expected function calls within concurrent Elixir processes.

This allows you to ensure that destructive and/or expensive functions are only called the expected number of times. For more information, see the Elixir forum post that motivated the development of this library.

Usage

defmodule ModuleTest do
  use ExUnit.Case, async: true

  import Tracee

  setup :verify_on_exit!

  describe "fun/0" do
    test "calls expensive function only once" do
      expect(AnotherModule, :expensive_fun, 1)

      assert Module.fun()
    end

    test "calls expensive function only once from another process" do
      expect(AnotherModule, :expensive_fun, 1)

      assert fn -> Module.fun() end
             |> Task.async()
             |> Task.await()
    end
  end
end

Summary

Functions

Sets an expectation for a function call with a specific arity and optional count.

Starts the tracer.

Verifies that all expected function calls have been received and nothing else.

Registers a verification check to be performed when the current test process exits.

Functions

Link to this function

expect(module, function, arity, count \\ 1)

View Source
@spec expect(module(), atom(), pos_integer(), non_neg_integer()) :: :ok

Sets an expectation for a function call with a specific arity and optional count.

Examples

  • Expect AnotherModule.expensive_fun/0 to be called once:

    Tracee.expect(AnotherModule, :expensive_fun, 0)
  • Expect AnotherModule.expensive_fun/1 to be called twice:

    Tracee.expect(AnotherModule, :expensive_fun, 1, 2)
  • Expect AnotherModule.expensive_fun/1 to be nerver called:

    Tracee.expect(AnotherModule, :expensive_fun, 1, 0)
@spec start_link() :: {:ok, pid()} | {:error, :already_started}

Starts the tracer.

@spec verify(pid()) :: :ok

Verifies that all expected function calls have been received and nothing else.

Link to this function

verify_on_exit!(context \\ %{})

View Source
@spec verify_on_exit!(map()) :: :ok

Registers a verification check to be performed when the current test process exits.

Examples

defmodule ModuleTest do
  use ExUnit.Case, async: true

  import Tracee

  setup :verify_on_exit!
end