Hammox v0.2.0 Hammox View Source

Hammox is a library for rigorous unit testing using mocks, explicit behaviours and contract tests.

See the README page for usage guide and examples.

Most of the functions in this module come from Mox for backwards compatibility. As of v0.1.0, the only Hammox-specific functions are protect/2 and protect/3.

Link to this section Summary

Link to this section Functions

Link to this function

allow(mock, owner_pid, allowed_via)

View Source

See Mox.allow/3.

See Mox.defmock/2.

Link to this function

expect(mock, name, n \\ 1, code)

View Source

See Mox.expect/4.

Link to this function

protect(mfa, behaviour_name)

View Source (since 0.1.0)
protect(mfa :: mfa(), behaviour_name :: module()) :: (... -> any())

Takes the function provided by a module, function, arity tuple and decorates it with Hammox type checking.

Returns a new anonymous function.

Example:

defmodule Calculator do
  @callback add(integer(), integer()) :: integer()
end

defmodule TestCalculator do
  def add(a, b), do: a + b
end

add_2 = Hammox.protect({TestCalculator, :add, 2}, Calculator)

add_2.(1.5, 2.5) # throws Hammox.TypeMatchError
Link to this function

protect(module_name, behaviour_name, funs)

View Source (since 0.1.0)
protect(
  module_name :: module(),
  behaviour_name :: module(),
  funs :: [{atom(), arity() | [arity()]}]
) :: map()

Same as protect/2, but allows decorating multiple functions at the same time.

Provide a list of functions to decorate as third argument.

Returns a map where the keys are atoms of the form :{function_name}_{arity} and values are the decorated anonymous functions.

Example:

defmodule Calculator do
  @callback add(integer(), integer()) :: integer()
  @callback add(integer(), integer(), integer()) :: integer()
  @callback multiply(integer(), integer()) :: integer()
end

defmodule TestCalculator do
  def add(a, b), do: a + b
  def add(a, b, c), do: a + b + c
  def multiply(a, b), do: a * b
end

%{
  add_2: add_2,
  add_3: add_3,
  multiply_2: multiply_2
} = Hammox.protect(TestCalculator, Calculator, add: [2, 3], multiply: 2)
Link to this function

set_mox_from_context(context)

View Source

See Mox.set_mox_from_context/1.

Link to this function

set_mox_global(context \\ %{})

View Source

See Mox.set_mox_global/1.

Link to this function

set_mox_private(context \\ %{})

View Source

See Mox.set_mox_private/1.

See Mox.stub/3.

See Mox.stub_with/2.

See Mox.verify!/0.

See Mox.verify!/1.

Link to this function

verify_on_exit!(context \\ %{})

View Source

See Mox.verify_on_exit!/1.