I’m testing some logic that invokes a function which looks like this:
defp publish_change(flag_name) do
if Config.should_emit_events? do
Task.start fn() ->
Redix.command(@conn, ["PUBLISH" | Notifications.payload_for(flag_name)])
end
end
end
I’m working with ExUnit on Elixir 1.4.1. I’m using the mock
library for mocks and call verifications. Yes, I’ve read the articles on why mocks are discouraged.
My problem is that the moment Task.start()
executes is not always immediate and my tests are not deterministic.
For example, I have a unit test where I assert that Redix.command
is invoked with the right arguments. The test passes if I test that file only, but fails if I run the entire test suite, and I think it’s because the BEAM schedulers are busy and the Task
is deferred.
I can make the test pass consistently with a simple :timer.sleep(10)
between the assertions, but I’m wondering if there is a better or more idiomatic way to deal with this.
Thanks!