Hammox v0.2.2 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
Functions
Takes the function provided by a module, function, arity tuple and decorates it with Hammox type checking.
Same as protect/2
, but allows decorating multiple functions at the same
time.
See Mox.verify!/0.
Link to this section Functions
See Mox.allow/3.
See Mox.defmock/2.
See Mox.expect/4.
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
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)
See Mox.set_mox_global/1.
See Mox.stub/3.
See Mox.stub_with/2.
See Mox.verify!/0.
See Mox.verify!/1.