📎 Webclip
Unit Tests in Elixir - Part 2
This article tackles how to unit test Elixir functions that communicate with another process, using a Persist module that talks to a KVStore GenServer as an example. The author introduces dependency injection via default module arguments so that tests can replace the real GenServer with a simple test double. Commands (cast-style calls) are tested by sending messages to the test process and asserting they were received, while queries (call-style calls) are tested by asserting on the return values of the mocked module.
Reading notes#
- The author continues from part 1, shifting focus from general unit test rules to specifics of testing behaviors that involve inter-process communication.
KVStoreis defined as aGenServerthat stores key/value pairs withwrite/2usingGenServer.cast/2andread/1usingGenServer.call/2.Persist.write_all/1andPersist.read_all/1are shown initially callingKVStoredirectly, which couples them to inter-process communication.- In the original
Persistversion, testing is hard because all behavior depends on talking toKVStore’s process. - The author dismisses adding a configurable adapter in
KVStorefor this scenario, considering it awkward and still difficult to test, though adapter patterns can suit other cases. - To enable unit testing, the author introduces a seam for dependency injection by adding a
kvstoreargument with a default value ofKVStorein bothwrite_all/2andread_all/2. - With default arguments, production code continues to use the real
KVStoremodule, while tests can pass in a test double module. - The modified
Persistkeeps its behavior but gains the ability to inject a dependency as a simple function argument. - The author notes that default arguments on every function make the API less visually appealing, but they make proper unit testing possible.
- In tests, a nested
KVStoremodule acts as a test double: itswrite/2sends a message toself(), and itsread/1returns a tagged tuple. - The test for
write_all/2callsPersist.write_all/2with a map and the test double module, then usesassert_receive/1to ensure the expected{:write, {key, value}}messages arrive in the test process. - The test for
read_all/2directly asserts thatPersist.read_all/2returns a list of the tagged tuples produced by the test double’sread/1. - The test double is not a
GenServer; instead, it hardcodes behaviors that make verification trivial for the tests. - The implementation’s
write/2is described as a command, andread/1as a query, borrowing terminology from object-oriented design. - A command corresponds to a
GenServer.cast/2where no response is expected and the receiving process privately decides what to do with the message. - For commands, the test’s responsibility is limited to verifying that the correct message was sent with the proper arguments, not what the other process does internally.
- This verification for commands is done by having the mock send a message to the test process and asserting that the test process received it.
- A query corresponds to a
GenServer.call/2where a response is expected from the receiving process. - For queries, the unit tests for the calling module only need to ensure the function is invoked with the right argument, which can be checked by asserting against the return value from the test double.
- The logic that determines what a real query should return for given input belongs in separate unit tests for the
KVStoremodule itself. - The author closes with three key guidelines for testing functions that talk to other processes: inject dependencies as function arguments, test commands via messages to
self(), and test queries via assertions on return values.
