From c287994a97d667ac72b2ae4405acc804adb6045d Mon Sep 17 00:00:00 2001 From: "Paulo F. Oliveira" Date: Tue, 2 Mar 2021 16:06:01 +0000 Subject: [PATCH] Add a further caveat to using `meck` with `ct` --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index dada3c3f..15c81e88 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,28 @@ changed changed ``` +### 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. + ## Contribute Patches are greatly appreciated! For a much nicer history, please [write good