View Source Dummy

Hex.pm Hexdocs

Elixir mocking that makes sense. Dummy relies on meck and exposes a simple way to mock methods, thanks to a couple of assumptions:

  • passthrough is enabled
  • mocked methods return their arguments
  • it's easy to specify replacements

Installing

    {:dummy, "~> 2.0", only: :test}

Usage

use ExUnit.Case
import Dummy

alias MyApp.Module


test "my test" do
    dummy OtherModule, ["method"] do
        Module.call_to_other_module("arg1")
        assert called(OtherModule.method("arg1"))
    end
end

Arities

test "my test" do
    dummy OtherModule, ["method/2"] do
        Module.call_to_other_module("arg1", "arg2")
        assert called(OtherModule.method("arg1", "arg2"))
    end
end

Specifying a return value

test "my test" do
    dummy OtherModule, [{"method", "value"}] do
        assert OtherModule.method("anything") == "value"
    end
end

Specifying a return value and an arity

test "my test" do
    dummy OtherModule, [{"method/2", "value"}] do
        assert OtherModule.method(1, 2) == "value"
    end
end

Specifying a replacement function

test "my test" do
    dummy OtherModule, [{"method", fn _x -> %{key: => "value"} end}] do
        assert OtherModule.method("anything") == %{key: => "value"}
    end
end

Multiple replacements

test "my test" do
    dummy OtherModule, ["method", "other_method"] do
        Module.call_to_other_module("arg1")
        assert called(OtherModule.method("arg1"))
        assert called(OtherModule.other_method("other_arg"))
    end
end

Disabling passthrough

test "my test" do
    dummy OtherModule, ["method"], passthrough: false do
        Module.call_to_other_module("arg1")
        assert called(OtherModule.method("arg1"))
    end
end