Make Tests Repeatable
All of our tests are inside our functional core. In the core, calling a function with the same arguments will almost always result in the same output. That word “almost” is a killer because our whole strategy involves comparing our expectations to actual values. When we can’t have expectations from run to run, we must change our approach.
Sometimes, functions are not perfectly pure. Functions that create timestamps or random numbers are famously difficult to test. We have both types of functions in our codebase. For example, recall the response test:
| test "a timestamp is added at build time", %{right: response} do |
| assert %DateTime{ } = response.timestamp |
| assert response.timestamp < DateTime.utc_now |
| end |
Get Designing Elixir Systems With OTP now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.