我在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()')
如果我监视所有其他服务,也会发生这种情况
解决方法
我在使用Jasmine 2.x版时看到了这个问题.对我来说问题是我正在调用andCallThrough()这是
Jasmine 1.x syntax.
2.x syntax是and.callThrough().