mockery v1.0.0 Mockery

Core functionality

Link to this section Summary

Functions

Function used to create mock in context of single test

Function used to prepare module for mocking

Link to this section Functions

Link to this function mock(mod, fun, value)

Function used to create mock in context of single test.

Mock created in one test won’t leak to another. It can be used safely in asynchronous tests.

Mocks can be created with value:

mock Mod, [fun: 2], "mocked value"

or function:

mock Mod, [fun: 2], fn(_, arg2) -> arg2 end

Keep in mind that function inside mock must have same arity as original one.

This:

mock Mod, [fun: 2], &to_string/1

will raise an error.

It is also possible to mock function with given name and any arity

mock Mod, :fun, "mocked value"

But this:

mock Mod, :fun, &string/1

doesn’t make any sense, because it will only work for Mod.fun/1.

Link to this function of(mod, opts \\ [])

Function used to prepare module for mocking.

For MIX_ENV other than :test it returns first argument unchanged. For test env it creates kind of proxy to oryginal module.

Proxy can be implicit

@module Mockery.of(MyApp.Module)

or explicit

@module Mockery.of(MyApp.Module, by: MyApp.FakeModule)

Explicit version is used for global mocks. For more information see Mockery.Heritage