View Source Hammox.Protect (Hammox v0.6.1)

A useable module simplifying protecting functions with Hammox.

The explicit way is to use Hammox.protect/3 and friends to generate protected versions of functions as anonymous functions. In tests, the most convenient way is to generate them once in a setup hook and then resolve them from test context. However, this can get quite verbose.

If you're willing to trade explicitness for some macro magic, doing use Hammox.Protect in your test module will define functions from the module you want to protect in it. The effect is similar to importing the module you're testing, but with added benefit of the functions being protected.

use Hammox.Protect supports these options:

  • :module (required) — the module you'd like to protect (usually the one you're testing in the test module). Equivalent to the first parameter of Hammox.protect/3 in batch usage.
  • :behaviour — the behaviour module you'd like to protect the implementation module with. Can be skipped if :module and :behaviour are the same module. Equivalent to the second parameter of Hammox.protect/3 in batch usage.
  • :funs — An optional explicit list of functions you'd like to protect. Equivalent to the third parameter of Hammox.protect/3 in batch usage.

Additionally multiple behaviour and funs options can be provided for modules that implement multiple behaviours

  • note: the funs options are optional but specific to the behaviour that precedes them
use Hammox.Protect,
  module: Hammox.Test.MultiBehaviourImplementation,
  behaviour: Hammox.Test.SmallBehaviour,
  # the `funs` opt below effects the funs protected from `SmallBehaviour`
  funs: [foo: 0, other_foo: 1],
  behaviour: Hammox.Test.AdditionalBehaviour
  # with no `funs` pt provided after `AdditionalBehaviour`, all callbacks
  # will be protected