O'Reilly logo

Mastering Angular Components by Gion Kunz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required