How can I avoid the warnings/errors when using a factory in a mock?

Having followed the blog post on “Mocks and explicit contracts” - http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/

Say that I have the MyApp.Twitter.InMemory module. This is primarily used for testing, but now lives under /lib. I added factories to /test/support and call the factory functions in the mock, but since the mock lives under /lib, I’m getting a warning ...undefined (module MyApp.Twitter.InMemory is not available. It still compiles, though.

I’m looking for suggestions as to how I can best organize code to avoid this warning. I’m leaning toward moving the mock (MyApp.Twitter.InMemory) to /test/support. Having attempted this, I’m receiving the following error:

10:26:24.129 [error] Task #PID<0.802.0> started from #PID<0.800.0> terminating
** (UndefinedFunctionError) function MyApp.Twitter.InMemory.get_tweet/1 is undefined (module My.App.InMemory is not available)
    MyApp.Twitter.InMemory.get_tweet(...)
    (elixir) lib/task/supervised.ex:85: Task.Supervised.do_apply/2
    (elixir) lib/task/supervised.ex:36: Task.Supervised.reply/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Function: #Function<4.6887132/0 in MyApp.Web.TweetController.show/2>
    Args: []

Any suggestions? Thoughts? Recommendations? Your input is greatly appreciated!

Some things you can double-check:

  • is "test/support" added to elixirc_paths: in your Mixfile? It’s only added in the :test environment by default in new projects.
  • is your Mock an .ex file (not .exs)?
  • Is the module name correct? The error mentions My.App.InMemory
1 Like