View Source Repatch
Repatch is a library for efficient, ergonomic and concise mocking/patching in tests (or not tests)
Features
Patch any function or macro (except NIF and BIF). Elixir or Erlang, private or public, it can be patched!
Designed to work with
async: true. Has 3 isolation levels for testing multi-processes scenarios.Requires no boilerplate or explicit DI. Though, you are completely free to write in this style with Repatch!
Every patch is consistent and applies to direct or indirect calls and any process you choose.
Powerful call history tracking.
superandrealhelpers for calling original functions.Works with other testing frameworks and even in environments like
iexor remote shell.Get async-friendly application env! with just a single line in test. See
Repatch.Application.
Installation
def deps do
[
{:repatch, "~> 1.0"}
]
endOne-minute intro
for ExUnit users
Add
Repatch.setup()into yourtest_helper.exsfile after theExUnit.start()use Repatch.ExUnitin your test moduleCall
Repatch.patchorRepatch.faketo change implementation of any function and any module.
For example
defmodule ThatsATest do
use ExUnit.Case, async: true
use Repatch.ExUnit
test "that's not a MapSet.new" do
Repatch.patch(MapSet, :new, fn ->
%{thats_not: :a_map_set}
end)
assert MapSet.new() == %{thats_not: :a_map_set}
assert Repatch.called?(MapSet, :new, 1)
end
endFurther reading
Please check out the docs for all available features.
Special thanks
To ihumanable for Patch library which was an inspiration and a good example for Repatch.