jjh42 / mock

Mocking library for Elixir language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about mocking child function

altyaper opened this issue · comments

Let's say I have something like this:

defmodule MyModule do

   def functionA do
      :rand.uniform(10)
   end

   def functionB do
      functionA() |> add
   end

   def add(num) do
      num + 1
   end

end

How can I mock functionA but doing an assert inside the macro, something like this

test "it should return 5 if random number is 4" do
   with_mock MyModule, [functionA: fn() -> 4 end] do
      assert MyModule.functionB() == 5
   end
end

I hope I can explain well my question here, I did a silly example but I need the answer in order to test a wrapper API I'm working on.

Thanks in advance.

I think what you need is the passthrough option.

Replace

with_mock MyModule, [functionA: fn() -> 4 end] do

with

with_mock MyModule, [:passthrough], [functionA: fn() -> 4 end] do