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 methods. The multiply
method simply multiplies two numbers and returns the result. The pythagorean
method calculates the hypotenuse of a right-angled triangle with two sides, a
and b
.
You might remember the formula for the Pythagorean...