jjh42 / mock

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

add a thin wrapper around :meck.passthrough #108

Closed azhi closed 4 years ago

azhi commented 4 years ago

As this is a thin wrapper, it doesn't perform any checks and has the same syntax of wrapping args in [].

Better macro that is available only inside anonymous mock functions and has nicer syntax for args could theoretically be possible, but would require much more work and will be much harder to maintain.

If you are interestered in that kind of macro, let me know - I can try and spend some time implementing it.

coveralls commented 4 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 351b8ee1981c1102b285cda90eca16b9246aaa43 on azhi:add-passthrough-in-mock-func into 77b5b6758991b348ecedb195cd884bf66a019bdb on jjh42:master.

Olshansk commented 4 years ago

I'm not really sure I understand this change. Is it not already supported?

See the second code snippet in this section: https://github.com/jjh42/mock/blob/master/README.md#not-supported---mocking-internal-function-calls

azhi commented 4 years ago

This change adds support for creating a mock that wraps a function - i.e. still calls function that it is mocking, but adds some logic to it. This is not supported by mock yet, mock only supports :passthrough option that allows to call original functions if no mock function was defined. :meck has separate :meck.passthrough/1 function that adds support for such a feature - docs can be found at https://hexdocs.pm/meck/ (passthrough/1).

Using module in https://github.com/jjh42/mock/blob/master/README.md#not-supported---mocking-internal-function-calls, an example for this feature would be creating a mock that still calls MyApp.IndirectMod.value/0, but adds 10 to it's return value:

defmodule MyTest do
  use ExUnit.Case, async: false
  import Mock

  test "indirect mock" do
    with_mocks([
      { MyApp.IndirectMod, [], [value: fn -> passthrough([]) + 10 end] },
    ]) do
      assert MyApp.IndirectMod.value() == 11
    end
  end
end
Olshansk commented 4 years ago

Got it. This is awesome, and I appreciate the extra explanation! :shipit: