我跟着
AngularJS documentation from here
问题是文档仅描述代码的“成功/快乐”分支,并且没有如何测试“失败”分支的示例。
我想做的是设置触发$ 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!'); }); });