Angular Unit Testing - Mocking methods/closures in same service Angular Unit Testing - Mocking methods/closures in same service angularjs angularjs

Angular Unit Testing - Mocking methods/closures in same service


When you stub a method on an object, the property of that object is overriden, not the original function that it references.

Take, for example, this code:

function myFunction () {};var myObj = { prop: myFunction };myObj.prop === myFunction; // truemyObj.prop = 'changed';typeof myFunction === 'function'; // truemyObj.prop === myFunction; // false

Changing myObj.prop did not change the original function, myFunction still exists in its own right. myObj.prop, however, has lost its reference to myFunction. If this was in sinon world, stubbing simply changed the reference of myObj.prop to a stub object.

This is why when testing code in a service which calls another function in the same service, that code needs to reference the same object returned by the service. If you want to avoid using the this keyword everywhere, you can structure your service like so:

angular.module('myApp')  .factory('peopleService', peopleService);peopleService.$inject = ['$q'];function peopleService ($q){  var service = {    getFamily: getFamily,    getAdults: getAdults  };  return service;  function getFamily () {     // ...  }  function getAdults (){    var family = service.getFamily(); // <-- reference service.getFamily()    // ...  }}