Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spyOn gives method does not exist error

I am running a Karma test on an angular app, in the test I have the following:

return inject(function($injector) {
   this.Service = {
      functionWithPromise: function(postdata){
         var deferred = $q.defer();
         deferred.resolve({
            data: {}
          });
          return deferred.promise;
         }
      };
};

and

it('should call the functionWithPromise function when the create function is called', function() {
    res = {}
    this.scope.create(res);
    this.scope.$digest();
    spyOn(Service, "functionWithPromise");
    expect(this.Service.functionWithPromise).toHaveBeenCalled();  
  });

when I run the test it gives this error:

functionWithPromise() method does not exist

How can I get the test to recognize the functionWithPromise() function?

like image 737
jmona789 Avatar asked Jul 13 '16 16:07

jmona789


Video Answer


1 Answers

Figured it out, I needed to spy on this.Service instead of service, like this:

spyOn(this.Service, "functionWithPromise");
like image 194
jmona789 Avatar answered Sep 22 '22 12:09

jmona789