1
私は、約束を返すサービスを呼び出すAngular1コントローラを持っています。コントローラにコントローラにメソッドを追加すると、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' }]);
});
});
Iがテストに合格コントローラから.catch()
を削除した場合。
助けてくれてありがとう! – Reustonium
あなたは大歓迎です。 – estus