Jasmine spyon方法返回undefined

时间:2014-12-08 17:28:08

标签: angularjs unit-testing jasmine karma-runner

我的SpyOn方法出现了未定义的错误

这就是我的想法

    '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();
    });
  });
});

我总是收到此错误

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

如果我同时监视所有其他服务

,也会发生这种情况

1 个答案:

答案 0 :(得分:25)

使用Jasmine 2.x版时我看到了这个问题。对我来说问题是我在调用andCallThrough() Jasmine 1.x syntax2.x syntaxand.callThrough()