Closed azhi closed 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
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
Got it. This is awesome, and I appreciate the extra explanation! :shipit:
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.