jjh42 / mock

Mocking library for Elixir language
MIT License
646 stars 81 forks source link

Cannot mock Logger.error/1 #125

Closed ghost closed 3 years ago

ghost commented 3 years ago

Considering this code:

    test "test mock" do
      with_mock Logger, [enable: fn(_) -> 42 end] do
        assert Logger.enable("msg") == 42
      end

      with_mock Logger, [error: fn(_) -> 42 end] do
        assert Logger.error("msg") == 42
      end
    end

The first assertion will pass but the second one will fail with Erlang error: {:undefined_function, {Logger, :error, 1}}. I suspect this is because Logger.error/1 is macro while Logger.enable/1 is a regular function. However I didn't see any mention of this limitation in the doc so I'm wondering if I might be missing something.

I can work around this by defining a wrapper function in my code that just calls Logger.error/1 and call/mock that wrapper function instead.

Olshansk commented 3 years ago

Yea, the limitation you mentioned is correct.

Could you send a PR to update the documentation?