ここからAngularJSのドキュメントに従っています
問題は、ドキュメントがコードの「成功/ハッピー」ブランチのみを説明しており、「失敗」ブランチをテストする方法の例がないことです。
私がやりたいことは、$scope.status = 'ERROR!'
コードをトリガーするための前提条件を設定することです。
これは最小限の例です。
// controller
function MyController($scope, $http) {
this.saveMessage = function(message) {
$scope.status = 'Saving...';
$http.post('/add-msg.py', message).success(function(response) {
$scope.status = '';
}).error(function() {
$scope.status = 'ERROR!';
});
};
}
// testing controller
var $httpBackend;
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
}));
it('should send msg to server', function() {
$httpBackend.expectPOST('/add-msg.py', 'message content').respond(500, '');
var controller = scope.$new(MyController);
$httpBackend.flush();
controller.saveMessage('message content');
$httpBackend.flush();
// Here is the question: How to set $httpBackend.expectPOST to trigger
// this condition.
expect(scope.status).toBe('ERROR!');
});
});