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 ...

Get Mastering JavaScript Functional Programming - Second Edition 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.