/mok

Function mock generator for Elixir

Primary LanguageElixirOtherNOASSERTION

mix test Hex version badge License badge

mok is small library to inject and mock functions easily.

Installation

The package can be installed by adding mok to your list of dependencies in mix.exs:

def deps do
  [{:mok, "~> 0.1"}]
end

Usage

inject, mock

  defmodule Target do
    require Mok

    def call(mocks) do
      a = [1, 2, 3] |> List.first() |> Mok.inject(mocks, "unmatched") # 1 (not injected)
      b = [1, 2, 3] |> List.first() |> Mok.inject(mocks, "matched") # 10 (injected)
      c = [1, 2, 3] |> List.first() |> Mok.inject(mocks, nil) # 1 (not injected)
      d = [1, 2, 3] |> List.first() |> Mok.inject(mocks) # 100 (injected)

      a + b + c + d
    end
  end
require Mok

assert 4 == Target.call(%{})

assert 112 ==
          Target.call(
            Mok.mock(%{
              {&List.first/1, "matched"} => 10,
              &List.first/1 => 100
            })
          )

License

This project is licensed under the MIT License - see the LICENSE file for details