View Source Repatch.Mock (Repatch v1.6.0)

Helper to generate modules based on protocols or behaviours. See Repatch.Mock.define/2.

Summary

Types

Options passed in the define/2 function.

A tuple defining a function name and arity

Functions

Creates a module implementing all functions from passed behaviours. Useful for creating dummy implementations for behaviours or protocols which can be patched later.

Types

@type define_option() ::
  {:behaviour, [module()] | module()}
  | {:protocol, [module()] | module()}
  | {:defstruct_fields, [atom() | {atom(), any()}]}
  | {:reconsolidate, boolean()}
  | {:only, [signature()]}
  | {:except, [signature()] | :optional}

Options passed in the define/2 function.

  • behaviour (module or list of modules) — What modules to generate callback implementations for.
  • protocol (module or list of modules) — What protocols to implement. Passing this option will automatically generate a defstruct statement.
  • only (list of signatures) — What functions to generate.
  • except (:optional or list of signatures) — What functions to not generate.
  • reconsolidate (boolean) — Whether to recompile consolidated protocols or not. Default is false.
  • defstruct_fields (list) — Just a list of fields for defstruct statement. Default is [].
@type signature() :: {atom(), arity()}

A tuple defining a function name and arity

Functions

@spec define(atom(), [define_option()]) :: {:module, module(), binary(), term()}

Creates a module implementing all functions from passed behaviours. Useful for creating dummy implementations for behaviours or protocols which can be patched later.

Example

iex> define(Something, behaviour: Access)
iex> Something.fetch([], :x)
** (Repatch.Mock.NotImplemented) Not implemented
iex> Repatch.patch(Something, :fetch, fn kwd, key -> Keyword.get(kwd, key) end)
iex> Something.fetch([], :x)
nil

It is also possible to pass multiple behaviours or protocols at the same time:

iex> define(SomeModule,
...>   behaviour: GenServer,
...>   behaviour: :gen_statem,
...>   protocol: Enumerable,
...>   protocol: Collectable,
...>   reconsolidate: true
...> )

It is also advised to disable protocol consolidation when defining protocol implementations. However, if protocol consolidation is required during the test, you can pass the reconsolidate: true option.

It is recommended to call define/2 during test code compilation or at least in test_support.exs.