几个小时以来,我一直在尝试使用 $httpBackend 测试我的 NewPostController。问题是每当我在响应中设置非 2xx 状态代码时,测试都会失败。
NewPostController 有以下方法:
$scope.submit = function () {
var newPost = $scope.newPost;
PostService.insertPost(newPost).then(function (post) {
$location.path("/my-posts");
}, function (status) {
$scope.form.$setPristine();
$scope.error = status;
});
};
我在测试失败路径时遇到问题:
it(...) {
...
$scope.post.text = "";
$httpBackend.expectPOST("/create-post", {"post":$scope.post}).respond(400);
$scope.submit();
$httpBackend.flush();
expect($scope.error).toBeDefined();
$scope.post.text = "This is a valid text.";
$httpBackend.expectPOST("/create-post", {"post": $scope.post}).respond(200);
$scope.submit();
$httpBackend.flush();
expect($location.path()).toBe("/my-posts");
});
测试失败并显示消息“400 throwed”(没有调用堆栈)。我尝试更改子测试的顺序,使用 whenPOST 而不是 expectPOST 并像在 Angular 文档(https://docs.angularjs.org/api/ngMock/service/ $httpBackend )中那样组合这些方法,但没有成功。
请帮忙。
编辑:
现在,当我查看 PostService 时,“抛出 400”的来源是有道理的,但我希望错误由 angular 处理。因为这篇文章的“处理嵌套服务调用中的问题”一节,我把它扔了。它应该是 deferred.resolve/reject 机制的较短版本。
this.insertPost = function (newPost) {
return $http({
method: "post",
url: "/create-post",
data: {
post: newPost
}
}).then(function (res) {
return (res.data);
}, function (res) {
throw res.status;
});
};