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
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)
Define module
to be able to mock functions
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
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
Chooses the mode based on ExUnit context. If async
is true
then
the mode is private, otherwise global
setup :set_mimic_from_context
Sets the mode to global. Mocks can be set and used by all processes
setup :set_mimic_global
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 nodenode
.{:failover, node}
- used if the application is distributed and is started on the current node because of a failover on nodenode
, 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
.
To allow Calculator.add/2
to be called:
stub(Calculator, :add, fn x, y -> x + y end)
Verify if expectations were fulfilled for a process pid
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!