eproxus / meck

A mocking library for Erlang
http://eproxus.github.io/meck
Apache License 2.0
811 stars 231 forks source link

Add a further caveat to using `meck` with `ct` #225

Closed paulo-ferraz-oliveira closed 3 years ago

paulo-ferraz-oliveira commented 3 years ago

As discussed over Slack, we should probably add this caveat to the README.

eproxus commented 3 years ago

May I suggest this text instead?

### Common Test

When using `meck` under Erlang/OTP's Common Test, one should pay special
attention to this bit in the chapter on
[Writing Tests](https://erlang.org/doc/apps/common_test/write_test_chapter.html):

> `init_per_suite` and `end_per_suite` execute on dedicated Erlang processes,
> just like the test cases do.

Common Test runs `init_per_suite` in an isolated process which terminates when
done, before the test case runs. A mock that is created there will also
terminate and unload itself before the test case runs. This is because it is
linked to the process creating it. This can be especially tricky to detect if
`passthrough` is used when creating the mock, since it is hard to know if it is
the mock responding to function calls or the original module.

To avoid this, you can pass the `no_link` flag to `meck:new/2` which will unlink
the mock from the process that created it. When using `no_link` you should make
sure that `meck:unload/1` is called properly (for all test outcomes, or
crashes) so that a left-over mock does not interfere with subsequent test
cases.
paulo-ferraz-oliveira commented 3 years ago

Done: 6f45f1a.

eproxus commented 3 years ago

Will release a new version with this and #223 as soon as I have some spare time.

paulo-ferraz-oliveira commented 3 years ago

Sure thing, @eproxus. Thanks again.

eproxus commented 3 years ago

0.9.2 is released with this PR included.

paulo-ferraz-oliveira commented 3 years ago

Thanks much, Adam.