Testing Elixir: assert_receive throws "no matching message" error (page 85, B6.0)

First iteration that uses Process.sleep/1 works but the stab on using assert_receive doesn’t. I’ve even increased the timeout to 5,000 milliseconds for assert_receive but the test still fails with “no matching message”.

I can’t figure out what’s wrong with the test (for now). By the way, I’m using Mox 1.0 and Elixir 1.11.4.

If I’ve figured out, I’ll report back.

Okay, I’ve figured out:

  1. The test should not be in async mode, i.e., use ExUnit.Case, async: false. Also, instead of :set_mox_from_context, be explicit and use :set_mox_global (for simpler test body) or use allow/3 to grant the sharing (more verbose); refer to Mox — Mox v1.0.0 for more info.
  2. The (test) config file needs to reference the mocked version.

To ensure repeatability, SoggyWaffle.WeatherAPI.Behavior should also be stubbed to make SoggyWaffle.rain?/2 always return true.

HTH.