config :my_app,
# DI
twitter_client: MyApp.Mocks.TwitterClient
Everything worked while incrementally compiling during development, however on a fresh checkout without any _build folder i get this error:
== Compilation error in file test/support/mocks.ex ==
** (ArgumentError) module MyApp.TwitterClient is not available, please pass an existing module to :for
lib/mox.ex:92: Mox.validate_behaviour!/1
lib/mox.ex:84: Mox.defmock/2
(elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
if I remove that mock line, compile and add the line back everything works. It seems like it’s trying to compile the mock file first and then the lib folder.
My elixirc_paths is ["lib", "test/support", "test/factories"] so lib comes first, any idea?
The Mox documentation suggests putting the Mox.defmock calls in test/test_helper.exs, that’s because the test helper is evaluated after all files are compiled. When you put the calls in the body of a compiled file it will try to check the mocked module is available at compile time which is a race condition.
This is actually incorrect, and also something you probably don’t want to do as you will lose compile-time compatability check of behaviors.
The problem is slightly different - i.e. the behavior is not properly specified here. I will publish a pull request for your repo @alex88 in a few mins to show you.
That’s because there is no order in the compilation process?
Anyway, having the mocked module in the test config, when I move the mock into test_helper generates:
warning: function MyApp.Mocks.TwitterClient.configure/2 is undefined (module MyApp.Mocks.TwitterClient is not available)
lib/my_app/stream/publisher.ex:9
warning: function MyApp.Mocks.TwitterClient.update_with_media/2 is undefined (module MyApp.Mocks.TwitterClient is not available)
lib/my_app/stream/publisher.ex:22
warning: function MyApp.Mocks.TwitterClient.update/1 is undefined (module MyApp.Mocks.TwitterClient is not available)
lib/my_app/stream/publisher.ex:25
Yes, modules will be compiled in the correct based on the calls or requires that is made in compile time. If you call a module at compile time the calling module will wait until the callee is compiled.
If it’s supposed to be supported to define behaviour implementations* at compile time then I think this line mox/lib/mox.ex at master · dashbitco/mox · GitHub should call Code.ensure_compiled? instead.
It’s not supposed to define new behaviours at compile time. Rather that it should create behaviour-compatible mocks based on already compiled modules, but you are right, it should be Code.ensure_compield?
If you call Mox.defmock at compile time, which you do if you put it in the body of a file covered by elixirc_paths, then the function will call Code.ensure_loaded? at compile time. You will see that if you follow the stacktrace leading up to mox/lib/mox.ex at master · dashbitco/mox · GitHub. Calling Code.ensure_loaded? at compile is not safe because it’s a race to check if the module is loaded. If you instead call Code.ensure_compiled? then the compiler will wait until the given module is compiled, it works similar to require. You will notice that it works if you add a call to Code.ensure_compiled? before the call to Mox.defmock.
I am trying to figure out why this is not happening on the app I am using mox in. But I guess it simply boils down to me being lucky and compilation order being luckily correct for me.
I submitted a pull request and this should be resolved in next version :).
Mine had the problem only on clean builds, it took a while to find that was happening since I had the module to be mocked already compiled when I first added the dependency and everything was ok
I figured it out why it works for me. I have 3 apps in umbrella, and I only mock the interactions where one calls another. So I suppose in my case the clean build works since the app I am about to mock is already always fully compiled (is in_umbrella dependency).
I was experiencing this as well. My solution involved using use MyApp.MyBehaviour. This required that I implement def __using__(_) as so:
defmodule MyApp.MyBehaviour do
@moduledoc """
Behaviour definition
"""
@type entry_attrs_t::map
@callback create_entry(entry_attrs_t) :: any
def __using__(_) do
end
end
and then in test/support/mocks.ex:
use MyApp.MyBehaviour
defmodule Mocks do
@moduledoc false
end
Mox.defmock(MyApp.MyMock, for: MyApp.MyBehaviour)