Skip to content

ktec/mox_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example

This project demonstrates a problem using Mox with a supervised singleton style GenServer.

The problem can be briefly summarised as follows:

When a GenServer is added to the static application supervision tree, it will be started by default. If this GenServer depends on a service which you would like to replace with a mock in tests, this is only possible if your service is not invoked during the GenServer start up process, so start_link, init, handle_continue etc. If you attempt to call your service during any of these functions, whilst the code may work in production, in tests when a mock is defined with Mox.defmock call, it is already too late to define an expection.

Here in this project we have:

The mock is defined inside the test, but you could of course define the test in the test_helper.ex as suggested by the Mox README but this makes no difference to the result.

There are "work arounds" of course, but none are particularly wholesome.

  1. You could define your own TestMock module, which means it will be defined at the time of application boot.
  2. You could test for the Mix.env or function_exported? but uggg!!
  3. You could call mix test --no-start and manually start.

Question

  • What is your suggested approach to deal with this?
  • Are we testing the wrong abstraction?
  • Should we go with a hand crafted mock?
  • Are there any other options to solve this?

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages