Making Tests Repeatable—Continued
Explore methods to make tests repeatable in Elixir by handling randomness in your code. Understand how to use streams to generate random numbers and ensure your tests can consistently verify expected outcomes despite inherent randomness.
We'll cover the following...
Random numbers
We’ve successfully dodged the idea of random numbers, but it’s time to pay the piper. We have to solve for the fact that we don’t have repeatable results when we generate a question using random numbers, so we’ll have to improvise.
We can use streams to generate many random numbers, and then narrow that value to the one we need. We’ll be entering this now in /test/question_test.exs:
Let’s try this out by entering the following commands:
Note: If you restart the
iexterminal, you’ll have to enter the setup commands mentioned above again. Otherwise, ...