benonymus

benonymus

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

Hey,

I am using this mocking library:

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

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement