Testing impure functions
For starters, we'll go back to our getRandomLetter() function. With insider knowledge about its implementation (this is called white-box testing, as opposed to black-box testing, where we know nothing about the function code itself), we can spy (a Jasmine term) on the Math.random() method and set a mock function that will return whatever values we desire.
We can revisit some of the test cases that we went through in the previous section. In the first case, we set Math.random() to return 0.0001 (and test that it was actually called) and we also test that the final return was A. In the second case, just for variety, we set things up so that Math.random() can be called twice, returning two different values. We also verify ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access