mockery v2.3.1 Mockery.Macro View Source

Alternative macro-based way to prepare module for mocking/asserting.

Link to this section Summary

Functions

Function used to prepare module for mocking/asserting.

Link to this section Functions

Link to this macro

mockable(mod, opts \\ [])

View Source (macro)
mockable(mod :: module(), opts :: [{:by, module()}]) :: module()

Function used to prepare module for mocking/asserting.

For Mix.env other than :test it returns the first argument unchanged. If Mix.env equal :test it creates a proxy to the original module. When Mix is missing it assumes that env is :prod.

Examples

Prepare for mocking

defmodule Foo do
  import Mockery.Macro

  def foo do
    mockable(Bar).bar()
  end
end

Prepare for mocking with global mock

# test/support/global_mocks/bar.ex
defmodule BarGlobalMock do
  def bar, do: :mocked
end

# lib/foo.ex
defmodule Foo do
  import Mockery.Macro

  def foo do
    mockable(Bar, by: BarGlobalMock).bar()
  end
end

Mockery.of/2 comparison

  • It's based on macro and process dictionary instead of on tuple calls. (Tuple calls are disabled by default in OTP21+ and require additional compile flag to be reenabled)
  • It doesn't support passing module names as a string as it don't create unwanted compile-time dependencies between modules

Potential issues

Output of mockable/2 macro should not be bind to variable or module attribute. If it happens, you'll see a compilation warning at best, and in the worst case Mockery won't work correctly.