stubr v1.3.2 Stubr

Provides module stubs for Elixir. Module stubs can be created using stub!/1 and stub!/2. The input to this function is a keyword list of function names (expressed as atoms) and their implementations (expressed as anonymous functions):

[function_name: (...) -> any()]

Additionally, takes an optional keyword list to configure the stub.

Options

The options available to stub!/2 are:

  • :module - when set, if the module does not contain a function defined in the keyword list, then raises an UndefinedFunctionError

  • :auto_stub - when true and a module has been set, if there is not a matching function, then defers to the module function (defaults to false)

  • behaviour - when set, raises a warning if the stub does not implement the behaviour

  • call_info - when set, if a function is called, records the input and the output to the function. Accessed by calling __stubr__(:call_info: :function_name) (defaults to false)

Summary

Functions

Returns the call info of a stubbed module

Recieves a keyword list of function names and anonymous functions where all calls by the stub to a function in this list are deferred to the invocation of the anonymous function

Functions

call_info!(stub, function_name)

Returns the call info of a stubbed module.

Examples

iex> uniform_stub = [uniform: fn(_) -> 3 end]
iex> rand_stub = Stubr.stub!(uniform_stub, module: :rand, call_info: true)
iex> rand_stub.uniform(2)
3
iex> Stubr.call_info!(rand_stub, :uniform)
[%{input: [2], output: 3}]
stub!(functions, opts \\ [])

Recieves a keyword list of function names and anonymous functions where all calls by the stub to a function in this list are deferred to the invocation of the anonymous function.

Options

  • :module - when set, if the module does not contain a function defined in the keyword list, then raises an UndefinedFunctionError

  • :auto_stub - when true and a module has been set, if there is not a matching function, then defers to the module. (defaults to false)

  • behaviour - when set, raises a warning if the stub does not implement the behaviour

  • call_info - when set, if a function is called, records the input and the output to the function. Accessed by calling __stubr__(:call_info: :function_name) (defaults to false)

Examples

iex> uniform_stub = [uniform: fn(_) -> 3 end]
iex> rand_stub = Stubr.stub!(uniform_stub, module: :rand)
iex> rand_stub.uniform(2)
3
iex> rand_stub.uniform(4)
3