View Source Repatch.Expectations (Repatch v1.6.0)
Helpers for working with repatch in an imperative mocking style.
See expect/4
for more documentation.
Example
defmodule SuperComputerTest do
use ExUnit.Case, async: true
use Repatch.ExUnit, assert_expectations: true
import Repatch.Expectations
test "SuperComputer.meaning_of/1" do
expect(SuperComputer, :meaning_of, fn :life -> 42 end)
|> expect(fn 42 -> :life end)
assert SuperComputer.meaning_of(:life) == 42
assert SuperComputer.meaning_of(42) == :life
end
end
Summary
Functions
Performs cleanup of expectations queues. Returns true
if
queues were found and cleaned up. Returns false
if no expectations
queues were found
Queues up the patch which will be executed once (or multiple times) and then removed from queue. Once all patches are executed, calls to patched function will fail.
Checks if there are any expectations empty. Useful in asserts
Lists all not-yet executed expectations
Types
@type expect_option() :: {:times, pos_integer()} | Repatch.patch_option()
Options passed in the expect/4
function.
exactly
(positive integer oronce
) — If the function is expected to be called this exact amount of times. Defaults to1
.at_least
(non negative integer oronce
orany
) — If the function is expected to be called at least this amount of times. This option works similar to stubs in Mox library.ignore_forbidden_module
(boolean) — Whether to ignore the warning about forbidden module is being spied. Defaults tofalse
.force
(boolean) — Whether to override previously set patches to function. Defaults tofalse
.mode
(:local
|:shared
|:global
) — What mode to use for the patch. SeeRepatch.mode/0
for more info. Defaults to:local
.expectations_queue
(queue/0
) — The queue of expectations. It is optional
When no exacly
or at_least
options are specified, the default behaviour is exactly: 1
@type expectation() :: {:exactly, pos_integer(), (... -> any())} | {:at_least, non_neg_integer(), (... -> any())}
@opaque queue()
@opaque queues()
Functions
Performs cleanup of expectations queues. Returns true
if
queues were found and cleaned up. Returns false
if no expectations
queues were found
Example
iex> expect(DateTime, :utc_now, fn -> :ok end)
iex> cleanup()
true
See expect/4
@spec expect(module(), atom(), (... -> any())) :: queue()
@spec expect(queue(), [expect_option()], (... -> any())) :: queue()
See expect/4
@spec expect(module(), atom(), [expect_option()], (... -> any())) :: queue()
Queues up the patch which will be executed once (or multiple times) and then removed from queue. Once all patches are executed, calls to patched function will fail.
See expect_option/0
for all available options.
Example
iex> expect(DateTime, :utc_now, fn -> :first end)
iex> |> expect(fn -> :second end)
iex> |> expect(fn -> :third end)
iex> [DateTime.utc_now(), DateTime.utc_now(), DateTime.utc_now()]
[:first, :second, :third]
Notes
expect/4
supports isolation modes just likeRepatch.patch/4
does.When function called without any expectations empty, it will raise
Repatch.Expectations.Empty
.Calling
Repatch.patch/4
withforce: true
will override allexpect/2
calls in the current isolation.To validate the
exactly
andat_least
options, you can use theexpectations_empty?/0
function or useRepatch.ExUnit
withassert_expectations
option.
Checks if there are any expectations empty. Useful in asserts
Example
iex> expect(DateTime, :utc_now, [at_least: 1], fn -> 123 end)
iex> expectations_empty?()
false
iex> DateTime.utc_now()
iex> expectations_empty?()
true
@spec pending_expectations(queues()) :: [{key(), [expectation()]}]
Lists all not-yet executed expectations
Example
iex> func = fn -> :hello end
iex> expect(DateTime, :utc_now, func)
iex> pending_expectations()
[{{DateTime, :utc_now, 0}, [{:exactly, 1, func}]}]
iex> DateTime.utc_now()
iex> pending_expectations()
[]
Please note that at_least
expectations are always returned by the pending_expectations/0