edgurgel / mimic

A mocking library for Elixir
https://hexdocs.pm/mimic
Apache License 2.0
415 stars 34 forks source link

Mimic can't see a function when it's called internally without the module name. #39

Closed tiagodavi closed 3 months ago

tiagodavi commented 2 years ago

Hey Folks. I noticed Mimic is not able to see a function when this function is called without its module name. For example:

defmodule Transactions do 
  def delete(guids) do 
    delete_something(guids)
  end 

  def delete_something(guids) do 
    {:ok, guids}
  end
end

Mimic.copy(Transactions)

Transactions
|> expect(:delete_something, fn transaction_guids ->
  IO.inspect("Hey")
  {:ok, transaction_guids}
end)

Transactions.delete([])

** (Mimic.VerificationError) error while verifying mocks for #PID<0.2950.0>:

* expected Transactions.delete_something/1 to be invoked 1 time(s) but it has been called 0 time(s)

To fix this we need to call this way:

__MODULE__.delete_something(guids)

Can I open a PR to fix this? What do you folks think?

jimsynz commented 2 years ago

Hi there. This is a dupe of #27. Unfortunately it's impossible for Mimic to hook intra-module function calls because BEAM optimises this case and jumps directly to the appropriate bytecode.

edgurgel commented 2 years ago

Thanks, @jimsynz . As @jimsynz said there's not much we can do about that. The code must call the fully qualified name Transactions.delete_something or it won't be possible to be stubbed.

Maybe it's worth adding something to the README.md? :thinking: