Jasmine spam method returning undefined

I am getting undefined error in my SpyOn method

Here is what I came up with

    'use strict';
describe('WebApp: AccountController', function() {
  return beforeEach(function() {
    module('webApp');
    module('tpls');
    return inject(function($injector) {
      this.q = $injector.get('$q');
      this.userMock = {
        id: 1,
        email: 'pidsafs@gmail.com',
        name: 'Adones Pitogo'
      };
      this.modalMock = {
        open: (function(_this) {
          return function(tpl, ctrl) {
            _this.def = _this.q.defer();
            _this.def.resolve(true);
            return {
              result: _this.def.promise
            };
          };
        })(this)
      };
      this.teamMock = {
        id: 1
      };
      this.repoMock = {
        id: 1
      };
      this.httpBackend = $injector.get('$httpBackend');
      this.httpBackend.whenGET('/api/v1/session').respond({
        status: 'ok',
        user: this.userMock
      });
      this.httpBackend.whenGET("/api/v1/users/" + this.userMock.id + "/teams").respond({
        status: 'ok',
        teams: [this.teamMock]
      });
      this.httpBackend.whenGET("/api/v1/users/" + this.userMock.id + "/repositories/remote").respond({
        status: 'ok',
        repositories: []
      });
      this.httpBackend.whenGET("/api/v1/users/" + this.userMock.id + "/repositories/followed").respond({
        status: 'ok',
        repositories: []
      });
      this.InvoiceService = $injector.get('Common.InvoiceService');
      this.TeamService = $injector.get('Common.TeamService');
      this.RepositoryService = $injector.get('Common.RepositoryService');
      this.UserService = $injector.get('Common.UserService');
      this.rootScope = $injector.get('$rootScope');
      this.scope = this.rootScope.$new();
      this.scope.user = this.userMock;
      this.$controller = $injector.get('$controller');
      this.timeout = $injector.get('$timeout');
      this.compile = $injector.get('$compile');
      this.rootElement = $injector.get('$rootElement');
      this.toasterMock = {
        pop: function(type, title, message) {
          return true;
        }
      };
      this.modalSpy = spyOn(this.modalMock, 'open').andCallThrough();
      this.httpBackend.flush();
      $('body').append('<div id="profile-pic-input"></div>');
      this.openFileWindow = function() {
        return this.timeout(function() {
          return $('#profile-pic-input').trigger('click');
        });
      };
      this.controller = this.$controller('AccountController', {
        '$scope': this.scope,
        '$rootScope': this.rootScope,
        'Common.RepositoryService': this.RepositoryService,
        'Common.UserService': this.UserService,
        'Common.InvoiceService': this.InvoiceService,
        'toaster': this.toasterMock,
        'openFileWindow': this.openFileWindow,
        'Common.TeamService': this.TeamService
      });
      return this.scope.$digest();
    });
  });
});

      

I always get this error

TypeError: 'undefined' is not a function (evaluating 'spyOn(this.modalMock, 'open').andCallThrough()')

      

This happens if I also spied on all other services

+3


source to share


1 answer


I've seen this issue when using Jasmine version 2.x. The problem for me was what I was calling andCallThrough()

which is the Jasmine 1.x syntax . Syntax 2.x - and.callThrough()

.



+25


source







All Articles