I recently started using Mimic in my test suite. I have an intermittent test failure that looks similar to this:
1) test maybe_create_user/1 creates new user with api call to create_new_app_user/2 (MyApp.AccountsTest)
test/my_app/accounts_test.exs:643
** (ArgumentError) Function create_new_app_user/2 not defined for MyApp.API
code: |> Mimic.expect(:create_new_app_user, fn _, env ->
stacktrace:
(mimic 1.7.4) lib/mimic.ex:467: Mimic.raise_if_not_exported_function!/3
(mimic 1.7.4) lib/mimic.ex:230: Mimic.expect/4
test/my_app/accounts_test.exs:652: (test)
Looking at the Mimic source code here and then here, it is calling Kernal.function_exported?.
The docs on Kernal.function_exported? mention it can fail if the module is not loaded. That is the issue because the function does exist in the module. However, it only happens maybe 10% of the time.
Is there a better way to ensure the module is loaded instead of calling Code.ensure_loaded/1 before I call Mimic.expect/4
in my tests?