jjh42 / mock

Mocking library for Elixir language
MIT License
646 stars 81 forks source link

meck_proc already started #64

Closed Olshansk closed 7 years ago

Olshansk commented 7 years ago

I'm mocking the same module in two different test files.

If I set async: true in both of these test files, I get the following error:

  1) test <module_name> fails when <test_name>
     <file_name>:<line_num>
     ** (ErlangError) erlang error: {:already_started, #PID<0.886.0>}
     stacktrace:
       src/meck_proc.erl:95: :meck_proc.start(<mocked_module_name>, [])
       <file_name>:<line_num>: anonymous fn/2 in <test_file> <describe> fails <test_name>
       (elixir) lib/enum.ex:1623: Enum."-reduce/3-lists^foldl/2-0-"/3
       <file_name>:<line_num>: (test)

The workaround is to set async: false in at least one of the two files where the same module is being mocked.

Without investigating how :meck is implemented, I think I have a good guess as to why this is happening and understand that the root cause of the issue is the interdependence of my unit tests; I really shouldn't be mocking the same module in two different places.

Without refactoring my tests, do you have a suggestion for a workaround to have them run asynchronously?

Olshansk commented 7 years ago

@jjh42 Want to ping this back up. Would appreciate any help!

tompave commented 7 years ago

Hi @Olshansk, mocking happens at the global level and tests with mocks cannot run concurrently. Or, well, they can, but the behaviour will be nondeterministic.

From the readme:

Currently, mocking modules cannot be done asynchronously, so make sure that you are not using async: true in any module where you are testing.

Olshansk commented 7 years ago

@tompave That makes sense. Thanks for the reply!