如何在Angular控制器中测试错误回调,如下所示:
controller.js
Note
.query()
.$promise
.then(
function( notes ) {
// success
},
function() {
// error
}
);
在我的规范中,我可以在queryDeferred.resolve()
的调用中放入一些随机对象并测试成功回调。但是我该如何测试错误情况?
spec.js
//= require spec_helper
describe( 'NoteListCtrl', function() {
// edited for brevity...
beforeEach( inject( function( $controller ) {
NoteMock = {
query: function() {
queryDeferred = $q.defer();
return { $promise: queryDeferred.promise };
}
}
spyOn( NoteMock, 'query' ).andCallThrough();
}));
describe( 'Note.getNotes', function() {
describe ( 'Note.query', function() {
beforeEach( function() {
$scope.getNotes();
queryDeferred.resolve( ????? ); // What goes here?
$rootScope.$apply();
});
});
});
});
答案 0 :(得分:1)
使用reject
:
queryDeferred.reject(someData);