import createSpy = jasmine.createSpy; import createSpyObj = jasmine.createSpyObj; /** * Spy on a field which has a getter defined. * A getter is required as spying on a field of an object without a getter is not supported in jasmine. * @param spy Object on which you wish to spy. * @param fieldToSpyOn Name of the field (getter) you want to spy on. * @param callback The new return value of the field (getter). */ export function spyOnField(spy: any, fieldToSpyOn: string, callback: any): void { let oldFieldDescriptor = Object.getOwnPropertyDescriptor(spy, fieldToSpyOn); Object.defineProperty(spy, fieldToSpyOn, { set: oldFieldDescriptor.set, get: callback }); } /** * Creates a spy for a desired object on which we automatically add all functions present on the class to the spy. * @param spyName The name of the spy you are creating. * @param classToSpyOn The class of the spy you are creating. * @returns {any|T} Your newly created spy object. */ export function createSpyForObject(spyName: string, classToSpyOn: any): any { // Spy on all functions belonging to the specified class let functions = []; for (let i in classToSpyOn.prototype) { functions.push(i); } // Create the spy for the specified class return createSpyObj(spyName, functions); }