Javascript AngularJS Mocking$http返回错误
我在用Jasmine测试AngularJs中的HTTP帖子时遇到了一些问题 我有一个控制器,看起来是这样的:-Javascript AngularJS Mocking$http返回错误,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,我在用Jasmine测试AngularJs中的HTTP帖子时遇到了一些问题 我有一个控制器,看起来是这样的:- appControllers.controller("TaskAddController", function ($scope, $http) { $scope.task = {}; $scope.messages = {}; $scope.actions = { save : function() { $http.po
appControllers.controller("TaskAddController", function ($scope, $http) {
$scope.task = {};
$scope.messages = {};
$scope.actions = {
save : function() {
$http.post("/ajax/tasks/save", $scope.task)
.then(function() {
$scope.messages.success = true;
$scope.task = {};
});
}
};
});
describe("TaskAddController", function() {
var createController, scope, $httpBackend;
beforeEach(function () {
module('appControllers');
scope = {};
inject(function ($injector) {
$httpBackend = $injector.get("$httpBackend");
});
inject(function ($controller) {
createController = function () {
return $controller("TaskAddController", { $scope: scope });
};
});
});
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("when actions.save is called then should call service", function () {
var task = {
title: "Title",
description: "Description"
};
$httpBackend.expectPOST("/ajax/tasks/save", task);
createController();
scope.task = task;
scope.actions.save();
$httpBackend.flush();
});
});
我正在这样测试它:-
appControllers.controller("TaskAddController", function ($scope, $http) {
$scope.task = {};
$scope.messages = {};
$scope.actions = {
save : function() {
$http.post("/ajax/tasks/save", $scope.task)
.then(function() {
$scope.messages.success = true;
$scope.task = {};
});
}
};
});
describe("TaskAddController", function() {
var createController, scope, $httpBackend;
beforeEach(function () {
module('appControllers');
scope = {};
inject(function ($injector) {
$httpBackend = $injector.get("$httpBackend");
});
inject(function ($controller) {
createController = function () {
return $controller("TaskAddController", { $scope: scope });
};
});
});
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("when actions.save is called then should call service", function () {
var task = {
title: "Title",
description: "Description"
};
$httpBackend.expectPOST("/ajax/tasks/save", task);
createController();
scope.task = task;
scope.actions.save();
$httpBackend.flush();
});
});
这导致我得到以下错误错误:没有待处理的刷新请求代码>
我做错了什么
谢谢。您使用的是什么版本的AngularJS
当我运行代码时,我得到:错误:没有定义响应代码>
当我添加响应时,测试通过:
$httpBackend.expectPOST("/ajax/tasks/save", task).respond({});
非常感谢。我真不敢相信我错过了。我在深夜尝试做事,真是罪有应得!