Mimic v0.1.0 Mimic

Mimic is a library that simplifies the usage of mocks.

Modules need to be prepared so that they can be used.

You must first call copy in your test_helper.exs for each module that may have the behaviour changed.

Mimic.copy(Calculator)

ExUnit.start()

Calling copy will not change the behaviour of the module.

The user must call stub/3 or expect/3 so that the functions will behave differently.

Link to this section Summary

Functions

Allows other processes to share expectations and stubs defined by another process

Define module to be able to mock functions

To expect Calculator.add/2 to be called

To expect that Calculator.add/2 will not be called

Chooses the mode based on ExUnit context. If async is true then the mode is private, otherwise global

Sets the mode to global. Mocks can be set and used by all processes

Sets the mode to private. Mocks can be set and user by the process

Called when an application is started

To allow Calculator.add/2 to be called

Verify if expectations were fulfilled for a process pid

Verifies the current process after it exits

Link to this section Functions

Link to this function allow(module, owner_pid, allowed_pid)

Allows other processes to share expectations and stubs defined by another process.

Examples

To allow other_pid to call any stubs or expectations defined for Calculator:

allow(Calculator, self(), other_pid)
Link to this function copy(module)
copy(atom()) :: :ok

Define module to be able to mock functions

Link to this function expect(module, fn_name, num_calls \\ 1, func)
expect(atom(), atom(), non_neg_integer(), function()) :: module()

To expect Calculator.add/2 to be called:

expect(Calculator, :add, fn x, y -> x + y end)

If this function is not called the verification step will raise

Link to this function reject(func)
reject(function()) :: module()

To expect that Calculator.add/2 will not be called:

reject(&Calculator.add/3)

If this function is not called the verification step will raise

Link to this function set_mimic_from_context(context)

Chooses the mode based on ExUnit context. If async is true then the mode is private, otherwise global

setup :set_mimic_from_context
Link to this function set_mimic_global(context \\ %{})

Sets the mode to global. Mocks can be set and used by all processes

setup :set_mimic_global
Link to this function set_mimic_private(context \\ %{})

Sets the mode to private. Mocks can be set and user by the process

setup :set_mimic_private

Called when an application is started.

This function is called when an application is started using Application.start/2 (and functions on top of that, such as Application.ensure_started/2). This function should start the top-level process of the application (which should be the top supervisor of the application’s supervision tree if the application follows the OTP design principles around supervision).

start_type defines how the application is started:

  • :normal - used if the startup is a normal startup or if the application is distributed and is started on the current node because of a failover from another node and the application specification key :start_phases is :undefined.
  • {:takeover, node} - used if the application is distributed and is started on the current node because of a failover on the node node.
  • {:failover, node} - used if the application is distributed and is started on the current node because of a failover on node node, and the application specification key :start_phases is not :undefined.

start_args are the arguments passed to the application in the :mod specification key (e.g., mod: {MyApp, [:my_args]}).

This function should either return {:ok, pid} or {:ok, pid, state} if startup is successful. pid should be the PID of the top supervisor. state can be an arbitrary term, and if omitted will default to []; if the application is later stopped, state is passed to the stop/1 callback (see the documentation for the c:stop/1 callback for more information).

use Application provides no default implementation for the start/2 callback.

Callback implementation for Application.start/2.

Link to this function stub(module, fn_name, func)
stub(atom(), atom(), function()) :: module()

To allow Calculator.add/2 to be called:

stub(Calculator, :add, fn x, y -> x + y end)

Link to this function verify!(pid \\ self())

Verify if expectations were fulfilled for a process pid

Link to this function verify_on_exit!(context \\ %{})

Verifies the current process after it exits.

If you want to verify expectations for all tests, you can use verify_on_exit!/1 as a setup callback:

setup :verify_on_exit!