jasmine Spies Creating a new spy


Example

We can use jasmine.createSpy() to create a standalone spy. This is often useful if we need to pass a function as a callback to another function and want to test how it is used.

// source code
function each(arr, fn) {
    arr.forEach(fn);
}

// test code
describe('each', function() {
  let mockFn = jasmine.createSpy();

  it('calls a function for each item in the array ', function() {
    let arr = [1,2,3,4,5]
    each(arr, mockFn);
    expect(mockFn.calls.count()).toBe(arr.length);
  })
});