Skip to content

Not working, at least not in the way I expected #71

@naps62

Description

@naps62

I'm finding that this package does not work when mocking functions that are called indirectly. Here's the example I came up with to reproduce:

defmodule MyApp.MyMod do
  def value do
    1
  end

  def indirect_value do
    value()
  end
end

and here are some assertions I tried to make for it:

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

  alias MyApp.MyMod

  test "test" do
    # these two work, obviously
    assert MyMod.value == 1
    assert MyMod.indirect_value == 1

    # in this block, the second assertion fails with:
    # (UndefinedFunctionError) function MyApp.MyMod.indirect_value/0 is undefined (module MyApp.MyMod is not available)
    # I guess this is to be expected, although unintuitive
    with_mock MyMod, [value: fn -> 2 end] do
      assert MyMod.value == 2
      assert MyMod.indirect_value == 2
    end

    # and in this block (adding :passthrough), the second assertion fails, because the indirect_value function still returns 1
    with_mock MyMod, [:passthrough], [value: fn -> 2 end] do
      assert MyMod.value == 2
      assert MyMod.indirect_value == 2
    end
  end
end

I'm not sure how I should use the package to test this kind of calls, if that's at all possible

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions