Spying on component output

In testing, a common practice is to spy on function calls during the execution of tests, and then evaluate these calls, checking whether all functions were called correctly.

Jasmine provides us with some nice helpers, in order to use spy function calls. We can use the spyOn function of Jasmine to replace the original function with a spy function. The spy function will record any calls, and we can evaluate how many times they were called, and with what parameters.

Let's look at a simple example of how to use the spyOn function:

class Calculator { 
  multiply(a, b) { 
    return a * b; 
  } 
   
  pythagorean(a, b) { 
    return Math.sqrt(this.multiply(a, a) + this.multiply(b, b)); 
  } 
} 

We will test a simple calculator class that has two ...

Get Mastering Angular Components 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.