Patch.Mock (patch v0.8.1) View Source

Link to this section Summary

Types

What exposures should be made in a module.

The exposes option controls if any private functions should be exposed.

This history_limit option controls how large of a history a mock should store

Sum-type of all valid options

Functions

Returns the number of times a matching call has been observed

Checks to see if the call has been observed.

Checks to see if a function with the given name has been called in the given module.

Expose private functions in a module.

Gets the call history for a module.

Given a call finds the latest call that matched.

Decorates the history with whether or not the call in the history matches the provided call.

Returns all the calls in the history that match the provided call.

Mocks the given module.

Registers a mock value for a function.

Restores a module to pre-patch functionality.

Link to this section Types

Specs

exposes() :: :all | :public | Patch.Mock.Code.exports()

What exposures should be made in a module.

  • :public will only expose the public functions
  • :all will expose both public and private functions
  • A list of exports can be provided, they will be added to the :public functions.

Specs

exposes_option() :: {:exposes, exposes()}

The exposes option controls if any private functions should be exposed.

The default is :public.

Link to this type

history_limit_option()

View Source

Specs

history_limit_option() :: {:history_limit, non_neg_integer() | :infinity}

This history_limit option controls how large of a history a mock should store

It defaults to :infinity which will store an unlimited history.

Specs

Sum-type of all valid options

Link to this section Functions

Link to this macro

call_count(call)

View Source (macro)

Specs

call_count(call :: Macro.t()) :: Macro.t()

Returns the number of times a matching call has been observed

The call arguments support any valid patterns.

This function uses the Mock's history to check, if the history is limited or disabled then calls that have happened may report back as never having happened.

Specs

called?(call :: Macro.t()) :: Macro.t()

Checks to see if the call has been observed.

The call arguments support any valid patterns.

This function uses the Mock's history to check, if the history is limited or disabled then calls that have happened may report back as never having happened.

Specs

called?(module :: module(), name :: atom()) :: boolean()

Checks to see if a function with the given name has been called in the given module.

This function uses the Mock's history to check, if the history is limited or disabled then calls that have happened may report back as never having happened.

Specs

expose(module :: module(), exposes :: exposes()) :: :ok | {:error, term()}

Expose private functions in a module.

If the module is not already mocked, calling this function will mock it.

Specs

history(module :: module()) :: Patch.Mock.History.t()

Gets the call history for a module.

If the module is not already mocked, this function returns an empty new history.

Link to this macro

latest_match(call)

View Source (macro)

Specs

latest_match(call :: Macro.t()) :: Macro.t()

Given a call finds the latest call that matched.

Returns {:ok, {function, arguments}} if a matching call is found, false otherwise.

Link to this macro

match_history(call)

View Source (macro)

Specs

match_history(call :: Macro.t()) :: Macro.t()

Decorates the history with whether or not the call in the history matches the provided call.

Provided call arguments support any valid patterns.

Returns the calls descending (newest first) as a two-tuple in the form

{boolean(), {atom(), [term()]}}

The first element indicates whether the call matches the provided call.

The second element is a tuple of function name and arguments.

This macro uses the Mock's history to check, if the history is limited or disabled then calls that have happened may report back as never having happened.

Specs

matches(call :: Macro.t()) :: Macro.t()

Returns all the calls in the history that match the provided call.

Provided call arguments support any valid patterns.

Returns the calls descending (newest first) as the list of arguments in the call

This macro uses the Mock's history to check, if the history is limited or disabled then calls that have happened may report back as never having happened.

Link to this function

module(module, options \\ [])

View Source

Specs

module(module :: module(), options :: [option()]) ::
  {:ok, pid()} | {:error, term()}

Mocks the given module.

Mocking a module accepts two options, see the option/0 type in this module for details.

Link to this function

register(module, name, value)

View Source

Specs

register(module :: module(), name :: atom(), value :: Patch.Mock.Value.t()) ::
  :ok

Registers a mock value for a function.

If the module is not already mocked, this function will mock it with no private functions exposed.

Specs

restore(module :: module()) :: :ok

Restores a module to pre-patch functionality.

If the module is not already mocked, this function no-ops.