Thau/defmock

[idea] Add Defmock.called_with?

zorbash opened this issue · 1 comments

A user of the library might want to assert that the mock was called with some arguments.

defmodule TestedModule do
    def function_with_external_api(external_api) do
      case external_api.call_me(%{url: "https://google.com", 
                                                     params: %{q: "tesla model 3"},
                                                     method: :get}) do
        %{status_code: 200} -> :ok
        %{status_code: 404} -> :not_found
      end
    end
end

defmodule Test do
  use ExUnit.Case
  import Defmock

  test "ensure that external API is being called" do
    external_api = defmock(call_me: %{status_code: 200})
    TestedModule.function_with_external_api(external_api)

    assert external_api.called_with?(%{url: "https://google." <> _tld, params: _, method: :get})
  end 
end
Thau commented

v0.2.0 adds called_with?, but I'm going to keep the issue open because I want to add pattern matching on it to make more powerful. Right now, you need to pass the full list of parameters for it to work, there's no way to ignore some of them if you don't care, so... Still some work to do!