Spies Like Us (and We Like Spies)

When testing a function that does not have input or output, you can’t test that the output is what you expect, so you usually want to test that the code did what you expected it to do. For example, the main function takes no input and returns no output, so we want to test that main has called all the functions that it is supposed to call. In Jasmine, you can test that a function has been called by “spying” on the function. Spying on a function lets you know whether that function has been called, how many times it has been called, what arguments it was called with, and so on. Spying is not really useful in application code, but is great for unit testing. Let’s test main by spying on the functions that it is supposed ...

Get Learning to Program 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.