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 or once) — If the function is expected to be called this exact amount of times. Defaults to 1.
  • at_least (non negative integer or once or any) — 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 to false.
  • force (boolean) — Whether to override previously set patches to function. Defaults to false.
  • mode (:local | :shared | :global) — What mode to use for the patch. See Repatch.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())}
@type key() :: {module(), atom(), arity()}
@opaque queue()
@opaque queues()

Functions

Link to this function

cleanup(queues \\ Process.get(:repatch_expectations_queues))

View Source
@spec cleanup(queues()) :: boolean()

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
@spec expect(queue(), (... -> any())) :: queue()

See expect/4

Link to this function

expect(module, name, func)

View Source
@spec expect(module(), atom(), (... -> any())) :: queue()
@spec expect(queue(), [expect_option()], (... -> any())) :: queue()

See expect/4

Link to this function

expect(module, name, opts, func)

View Source
@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

Link to this function

expectations_empty?(queues \\ Queues.all())

View Source
@spec expectations_empty?(queues()) :: boolean()

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
Link to this function

pending_expectations(queues \\ Queues.all())

View Source
@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