appunite / mockery

Simple mocking library for asynchronous testing in Elixir.
https://hex.pm/packages/mockery
Apache License 2.0
91 stars 10 forks source link

Elixir compile emits warning when providing variable to assert_called list of arguments #32

Closed esse closed 5 years ago

esse commented 5 years ago

Example code:

defmodule SomeModule do
  def execute(a), do: a
end

defmodule SomethingOther do
  import Mockery.Macro

  def new(params), do: mockable(SomeModule).execute(params)
end

Example test:

defmodule TestAppTest do
  use ExUnit.Case
  use Mockery

  describe "some context" do
    setup do
      {:ok, params: %{a: 1}}
    end

    test "it should not give a warning here", %{params: params} do
      mock(SomeModule, [execute: 1], %{"some_key" => "some value"})

      SomethingOther.new(params)

      assert_called(SomeModule, :execute, [params])
    end
  end
end

Expected result:

Actual result:

(note that line 15 is: assert_called(SomeModule, :execute, [params]))

amatalai commented 5 years ago

Hi

I think that the warning is correct, in order to match a pattern you need to pin the variable. Try the following code:

assert_called(SomeModule, :execute, [^params])
esse commented 5 years ago

@amatalai thanks! I've created a PR which add that to README: #33.