In Elixir, how to mock a function that was defined in a 'used' module?

1.4k Views Asked by At

I'm using meck and it works great in most cases. However, I encountered the following error: elixir (ErlangError) erlang error: {:undefined_function, {OAuth2.Strategy.AuthCode, :new, 2}}

I found that :new was a function defined in OAuth2.Strategy's __using__ macro. That means :new wasn't defined OAuth2.Strategy.AuthCode. How do I mock out :new in this case?

1

There are 1 best solutions below

0
On

Meck creates mocks in runtime, thus it will never be able to mock macros in Elixir. If your macro eventually creates a function, that is then compiled into a module, you should be able to mock that (provided that you know the name of the function).