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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 18 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hassan
Running
mix hex.outdatedevery so often surfaces those thingsciroque
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)
ciroque
I was experiencing this as well. My solution involved using
use MyApp.MyBehaviour. This required that I implementdef __using__(_)as so:and then in
test/support/mocks.ex:kinda hacky, but not horrible…
hubertlepicki
Thank you both @alex88 for reporting the issue and @ericmj for finding the solution <3
alex88
Just as reference for readers, the pull request @hubertlepicki openend that includes the fix is Use Code.ensure_compiled? vs Code.ensure_loaded? to ensure the module is available by hubertlepicki · Pull Request #17 · dashbitco/mox · GitHub
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
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
I am trying to figure out why this is not happening on the app I am using
moxin. 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
I can confirm that changing from
Code.ensure_loaded?toCode.ensure_compiled?fixes the issue while compiling and the app tests passeshubertlepicki
Yes, you are right ref the cause of the issue.