Tutorial by Examples

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 the...
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 m...
In this example we have a service, let's call it search service that has a method called search() which will initiate a get request to a back end API. function SearchService($http) { const service = {}; service.search = function() { return $http({method: 'GET', url: `/api/s...
function calculatorService() { const service = {}; service.add = function(a,b) { return a + b } return service; } angular.module('app').factory('calculatorService', calculatorService); Testing describe('calculator service', function() { var calcula...
const foop = { get value() {}, set value(v) {} }; it('can spy on getter', () => { spyOnProperty(foop, 'value', 'get').and.returnValue(1); expect(foop.value).toBe(1); }); it('and on setters', () => { const spiez = spyOnProperty(foop, 'value', 'set'); foop.v...

Page 1 of 1