mockth

Functions

pub fn expect0(
  module module: String,
  function function: String,
  with fun: fn() -> a,
) -> Result(Bool, String)

Mock a function with zero argument. The function fun should return the value that the function will return when called.

pub fn expect1(
  module module: String,
  function function: String,
  with fun: fn(a) -> b,
) -> Result(Bool, String)

Mock a function with 1 argument

pub fn expect1_test() {
 let assert Ok(_) =
   mockth.expect1("gleam/function", "identity", fn(_) { "hello" })

 mockth.validate("gleam/function")
 |> should.equal(True)

 mockth.mocked()
 |> should.equal(["gleam/function"])

 function.identity("world")
 |> should.equal("hello")
}
pub fn expect2(
  module module: String,
  function function: String,
  with fun: fn(a, b) -> c,
) -> Result(Bool, String)

Mock a function with 2 arguments

pub fn expect3(
  module module: String,
  function function: String,
  with fun: fn(a, b, c) -> d,
) -> Result(Bool, String)

Mock a function with 3 arguments

pub fn expect4(
  module module: String,
  function function: String,
  with fun: fn(a, b, c, d) -> e,
) -> Result(Bool, String)

Mock a function with 4 arguments

pub fn expect5(
  module module: String,
  function function: String,
  with fun: fn(a, b, c, d, e) -> f,
) -> Result(Bool, String)

Mock a function with 5 arguments

pub fn mocked() -> List(String)

Returns the currently mocked modules.

pub fn unload(module: String) -> Result(Bool, String)

Unload a mocked module or a list of mocked modules. This will purge and delete the module(s) from the Erlang virtual machine. If the mocked module(s) replaced an existing module, this module will still be in the Erlang load path and can be loaded manually or when called.

pub fn unload_all() -> List(String)

The function returns the list of mocked modules that were unloaded in the process. Unloads all mocked modules from memory.

pub fn validate(module: String) -> Bool

Validate the state of the mock module(s). Validation can detect:

  • When a function was called with the wrong argument types (function_clause)
  • When an exception was thrown
  • When an exception was thrown and expected (via meck:exception/2), which still results in true being returned

Validation cannot detect:

  • When you didn’t call a function
  • When you called a function with the wrong number of arguments (undef)
  • When you called an undefined function (undef)
Search Document