Promox (Promox v0.1.0)

Documentation for Promox.

Link to this section Summary

Functions

Enables mock :for the given protocol.

Expects the protocol.name callback with arity given by code to be invoked with mock n times.

Initialize a new mock struct.

Allows the protocol.name callback with arity given by code to be invoked with mock any times. The call would be delegated to code and returns whatever code returns.

Verifys that all the expectations set for the mock have been called. Returns :ok if so; Otherwise, raises Promox.VerificationError.

Link to this section Functions

Link to this macro

defmock(list)

(macro)

Enables mock :for the given protocol.

Caveats

Since defmock/1 is a macro, so remember to require Promox before calling Promox.defmock/1.

require Promox

Promox.defmock(for: MyProtocol)
Link to this function

expect(mock, protocol, name, n \\ 1, code)

Expects the protocol.name callback with arity given by code to be invoked with mock n times.

Examples

To expect MyProtocol.callback/1 to be called once:

  my_mock =
    Promox.new()
    |> Promox.expect(MyProtocol, :callback, fn _mock -> :ok end)

To expect MyProtocol.callback/1 to be called five times:

  my_mock =
    Promox.new()
    |> Promox.expect(MyProtocol, :callback, 5, fn _mock -> :ok end)

Initialize a new mock struct.

my_mock = Promox.new()

# Promox.expect(my_mock, MyProtocol, :callback, fn _mock, ... -> ... end)

Since mocks are just isolated data structures, you can use them in concurrent processes.

  mock1 =
    Promox.new()
    |> Promox.expect(MyProtocol, :callback, fn _mock, ... -> :ok end)

  mock2 =
    Promox.new()
    |> Promox.expect(MyProtocol, :callback, fn _mock, ... -> :error end)

  assert :ok = MyProtocol.callback(mock1, ...)
  assert :error =
           fn -> MyProtocol.callback(mock2, ...) end
           |> Task.async()
           |> Task.await()
Link to this function

stub(mock, protocol, name, code)

Allows the protocol.name callback with arity given by code to be invoked with mock any times. The call would be delegated to code and returns whatever code returns.

Caveat

  1. The first argument passed to code is always the mock being stubbed.
  2. stub/4 will overwrite any previous calls to stub/4
  3. If expectations and stubs are defined for the same function and arity, the stub is invoked only after all expectations are fulfilled.

Examples

To allow MyProtocol.callback/1 to be called any times:

  my_mock =
    Promox.new()
    |> Promox.stub(MyProtocol, :callback, fn _mock -> :ok end)

Verifys that all the expectations set for the mock have been called. Returns :ok if so; Otherwise, raises Promox.VerificationError.