Text of patch.hexdocs.pm › Patch.html as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Patch — patch v0.16.1

View Source Patch (patch v0.16.1) Patch - Ergonomic Mocking for Elixir Patch makes it easy to mock one or more functions in a module returning a value or executing custom logic. Patches and Spies allow tests to assert or refute that function calls have been made. Using Patch is as easy as adding a single line to your test case. use Patch After this all the patch functions will be available, see the function documentation for details.

Summary Functions assert_any_call(call) Asserts that the given module and function has been called with any arity. assert_any_call(module, function) Asserts that the given module and function has been called with any arity. assert_called(call) Given a call will assert that a matching call was observed by the patched function. assert_called(call, count) Given a call will assert that a matching call was observed exactly the number of times provided by the patched function. assert_called_once(call) Given a call will assert that a matching call was observed exactly once by the patched function. debug(value \\ true) Enable or disable library level debugging. expose(module, exposes) Expose can be used to turn private functions into public functions for the purpose of testing them. fake(real_module, fake_module) Fakes out a module with an alternative implementation. history(module, sorting \\ :asc) Get all the observed calls to a module.

These calls are expressed as a {name, argument} tuple and can either be provided in ascending (oldest first) or descending (newest first) order by providing a sorting of :asc or :desc , respectively. inject(tag, target, keys, options \\ []) listen(tag, target \\ nil, options \\ []) Starts a listener process. patch(module, function, value) Patches a function in a module private(call) Suppress warnings for using exposed private functions in tests. private(argument, call) Suppress warnings for using exposed private functions in tests. real(module) Gets the real module name for a fake. refute_any_call(call) Refutes that the given module and function has been called with any arity. refute_any_call(module, function) Refutes that the given module and function has been called with any arity. refute_called(call) Given a call will refute that a matching call was observed by the patched function. refute_called(call, count) Given a call will refute that a matching call was observed exactly the number of times provided by the patched function. refute_called_once(call) Given a call will refute that a matching call was observed exactly once by the patched function. replace(target, keys, value) Convenience function for replacing part of the state of a running process. restore(module) Remove any mocks or spies from the given module restore(module, name) Remove any patches associated with a function in a module. spy(module) Spies on the provided module Functions Link to this macro assert_any_call(call) View Source (macro) @spec assert_any_call(call :: Macro.t ()) :: Macro.t () Asserts that the given module and function has been called with any arity. patch ( Example , :function , :patch ) assert_any_call Example . function # fails Example . function ( 1 , 2 , 3 ) assert_any_call Example . function # passes Link to this function assert_any_call(module, function) View Source @spec assert_any_call(module :: module (), function :: atom ()) :: nil Asserts that the given module and function has been called with any arity. patch ( Example , :function , :patch ) assert_any_call Example , :function # fails Example . function ( 1 , 2 , 3 ) assert_any_call Example , :function # passes This function exists for advanced use cases where the module or function are not literals in the test code.

If they are literals then assert_any_call/1 should be preferred. Link to this macro assert_called(call) View Source (macro) @spec assert_called( Macro.t ()) :: Macro.t () Given a call will assert that a matching call was observed by the patched function. This macro fully supports patterns and will perform non-hygienic binding similar to ExUnit's assert_receive/3 and assert_received/2 . patch ( Example , :function , :patch ) Example . function ( 1 , 2 , 3 ) assert_called Example . function ( 1 , 2 , 3 ) # passes assert_called Example . function ( 1 , _ , 3 ) # passes assert_called Example . function ( 4 , 5 , 6 ) # fails assert_called Example . function ( 4 , _ , 6 ) # fails Link to this macro assert_called(call, count) View Source (macro) @spec assert_called(call :: Macro.t (), count :: Macro.t ()) :: Macro.t () Given a call will assert that a matching call was observed exactly the number of times provided by the patched function.

This macro fully supports patterns and will perform non-hygienic binding similar to ExUnit's assert_receive/3 and assert_received/2 . Any binds will bind to the latest matching call values. patch ( Example , :function , :patch ) Example . function ( 1 , 2 , 3 ) assert_called Example . function ( 1 , 2 , 3 ) , 1 # passes assert_called Example . function ( 1 , _ , 3 ) , 1 # passes Example . function ( 1 , 2 , 3 ) assert_called Example . function ( 1 , 2 , 3 ) , 2 # passes assert_called Example . function ( 1 , _ , 3 ) , 2 # passes Link to this macro assert_called_once(call) View Source (macro) @spec assert_called_once(call :: Macro.t ()) :: Macro.t () Given a call will assert that a matching call was observed exactly once by the patched function.

This macro fully supports patterns and will perform non-hygienic binding similar to ExUnit's assert_receive/3 and assert_received/2 . patch ( Example , :function , :patch ) Example . function ( 1 , 2 , 3 ) assert_called_once Example . function ( 1 , 2 , 3 ) # passes assert_called_once Example . function ( 1 , _ , 3 ) # passes Example . function ( 1 , 2 , 3 ) assert_called_once Example . function ( 1 , 2 , 3 ) # fails assert_called_once Example . function ( 1 , _ , 3 ) # fails Link to this function debug(value \\ true) View Source @spec debug(value :: boolean ()) :: :ok Enable or disable library level debugging.

There is a suite level configuration that can be set by using config :patch , debug : true # or false Calling this helper will enable or disable debugging for a given test. Library level debugging can be useful when patch behavior isn't meeting expectations. Additional logging will occur at the debug log level using Logger to provide insight into how Patch is working. Link to this function expose(module, exposes) View Source @spec expose(module :: module (), exposes :: Patch.Mock.exposes ()) :: :ok | {:error, term ()} Expose can be used to turn private functions into public functions for the purpose of testing them.

To expose every private function as a public function, pass the sentinel value :all . expose ( Example , :all ) Otherwise pass a Keyword.t(arity) of the functions to expose. For example, if one wanted to expose private_function/1 and private_function/2 . expose ( Example , [ private_function : 1 , private_function : 2 ] ) After exposing a function, attempting to call the exposed function will cause the Elixir Compiler to flag calls to exposed functions as a warning.

There are companion macros private/1 and private/2 that test authors can wrap their calls with to prevent warnings. Link to this function fake(real_module, fake_module) View Source @spec fake(real_module :: module (), fake_module :: module ()) :: :ok Fakes out a module with an alternative implementation. The real module can still be accessed with real/1 . For example, if your project has the module Example.Datastore and there's a fake available in the testing environment named Example.Test.InMemoryDatastore the following table describes which calls are executed by which code before and after faking with the following call. fake ( Example.Datastore , Example.Test.InMemoryDatastore ) Calling Code Responding Module before fake/2 Responding Module after fake/2 Example.Datastore.get/1 Example.Datastore.get/1 Example.Test.InMemoryDatastore.get/1 Example.Test.InMemoryDatastore.get/1 Example.Tes

The rest of this page is on the live site.