Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a thin wrapper around :meck.passthrough #108

Merged
merged 1 commit into from
Sep 12, 2019

Conversation

azhi
Copy link
Contributor

@azhi azhi commented Sep 10, 2019

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
Copy link

Coverage Status

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

@Olshansk
Copy link
Collaborator

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
Copy link
Contributor Author

azhi commented Sep 11, 2019

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
Copy link
Collaborator

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

@Olshansk Olshansk merged commit 2145756 into jjh42:master Sep 12, 2019
@azhi azhi deleted the add-passthrough-in-mock-func branch September 12, 2019 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants