1
我有一個Angular1控制器調用一個返回promise的Service。當向控制器調用服務時添加.catch()
方法時,mocha會引發以下錯誤。測試包含.catch()方法的Promise時出錯
TypeError: undefined is not an object (evaluating 'DogService.getDogs(_this.userId)
.then(function(result){
_this.myDogs = result;
})
.catch') in app/scripts/controllers/main.js (line 20)
[email protected]/scripts/controllers/main.js:20:11
test/spec/controllers/main.js:33:20
[email protected]://localhost:8080/context.js:151:17
控制器
angular.module('testProblemApp').controller('MainCtrl', ['DogService', function (DogService) {
var _this = this;
_this.myDogs = [];
_this.userId = 1;
_this.init = function(){
DogService.getDogs(_this.userId)
.then(function(result){
_this.myDogs = result;
})
.catch(function(error){
console.log(error);
});
};
}]);
測試
describe('initialze function', function() {
it('should set the myDogs array to the value returned by the Service', function() {
spyOn(DogService, 'getDogs').and.callFake(function() {
return {
then: function (callback) {
return callback([{ id: 1, name: 'baxter' }]);
},
catch: function(callback){
return callback('Error');
}
}
});
MainCtrl.init();
expect(MainCtrl.myDogs).toEqual([{ id: 1, name: 'baxter' }]);
});
});
如果刪除從測試通過控制器的.catch()
。
感謝您的幫助! – Reustonium
不客氣。 – estus