benonymus

benonymus

Mocking the same function in 2 separate tests while async: false makes test fail

Hey,

I am using this mocking library:
https://github.com/jjh42/mock

and I am experience something weird:

I have 2 test cases in 2 different folders in the test folder that mock the same function and then asserts that this function has been called.

with_mock(CoolModule, do_something: fn _ -> :ok end

and then at the end

assert_called(CoolModule.do_something(2))

One of the tests, always the same fails if I run the tests together.

If I run the failing test by itself then it passes, or if I comment out the other test that also makes the test pass.

If I put a sleep in the failing test that also makes it pass.

I have async: false set on the test module.

According to the docs of Mock this should work.

Any ideas why this could be?

Thanks

Most Liked

al2o3cr

al2o3cr

The overall vibe of these symptoms is “there is something leaking from the execution of one test and affecting the other test”, since they go away when run in isolation.

“If I put a sleep in the failing test that also makes it pass” sounds like a race: something that cleans up the first test is running in parallel with the second test, and if the second test beats the cleanup you get a failure.

I don’t see anything obviously asynchronous in the mock source - this issue mentions switching to ExUnit.at_exit instead of after, but the suggestion was not implemented.

thbar

thbar

Hi!

The best advice I can provide here is to, if possible, switch to using behaviours and Mox, following the advice outlined in Mocks and explicit contracts - Dashbit Blog, because this allows to keep async tests, and uses per-process expectations (even if it can be a bit harder to use in some contexts, in my experience), with clear boundaries.

On your specific question, also, without more code it will probably be a bit difficult to provide some help. If you have a way to share a smaller reproduction (and I know it can be hard to do), you will get more chance here! (or a bit more concrete elements).

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement