jasmine Spies Spying on an existing function


Example

Jasmine can spy on an existing function using the spyOn function.

let calculator = {
  multiply: function(a, b) {
     return a * b;
  },

  square: function(a) {
    return this.multiply(a, a);
  }
}


describe('calculator', function() {
  it('squares numbers by multiplying them by themselves', function() {
    let num = 2;
    spyOn(calculator, 'multiply');
    calculator.square(NUM);
    expect(calculator.multiply).toHaveBeenCalledWith(NUM, NUM);
  })
});

After the function has been spied on it is replaced with a spy, that can be queried for information about how and when it has been called.