lundi 27 juin 2016

Can't mock a factory method that calls another factory method

I have a factory like this

angular.module('app')
.factory('Utils', function () {
   function one() {
   }

   function two() {
     one();
   }

   return {
     one: one,
     two: two
 });
});

In a jasmine spec I attempt to do something like this:

it('should verify', inject(function(Utils) {
  spyOn(Utils, 'one');
  Utils.two();
  expect(Utils.one).toHaveBeenCalled();
}));

However I get an error saying the spy was never called. I guess it some sort of reference issue. Any idea why I can't spyOn a factory function that is called from another function in the same factory?

Aucun commentaire:

Enregistrer un commentaire