alex88

alex88

I’m trying to mock ExTwitter during tests, I’m using GitHub - dashbitco/mox: Mocks and explicit contracts in Elixir · GitHub for that.

In my code I use a config variable to do the Dependency Injection, like this:

config :my_app,
  # DI
  twitter_client: MyApp.TwitterClient

I’ve defined my mock in test/support/mocks.ex as per Mox docs like this

Mox.defmock(MyApp.Mocks.TwitterClient, for: MyApp.TwitterClient)

and in my test.exs config I have

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?

I’ve pushed a sample app with the problem in https://github.com/alex88/myapp just run mix test

Showing Posts 18 to 9

hassan

hassan

Turns out the real solution was to upgrade the version
Didn’t realize I had pinned it at 0.1.0 (C&P from web page, DOH)

Running mix hex.outdated every so often surfaces those things :slight_smile:

ciroque

ciroque

Turns out the real solution was to upgrade the version :wink:
Didn’t realize I had pinned it at 0.1.0 (C&P from web page, DOH)

ciroque

ciroque

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)

kinda hacky, but not horrible…

hubertlepicki

hubertlepicki

Thank you both @alex88 for reporting the issue and @ericmj for finding the solution <3

hubertlepicki

hubertlepicki

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).

alex88

alex88 OP

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

hubertlepicki

hubertlepicki

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 :).

alex88

alex88 OP

I can confirm that changing from Code.ensure_loaded? to Code.ensure_compiled? fixes the issue while compiling and the app tests passes

hubertlepicki

hubertlepicki

Yes, you are right ref the cause of the issue.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews