【发布时间】:2015-09-28 11:32:24
【问题描述】:
我在 http 请求中创建了一个函数,用于获取然后发布请求我如何测试该方法这里是我的指令控制器函数:
var getConfig = function () {
if (!$scope.json) {
$http.get('/schools/' + $scope.school.id + '/config?deployment_uuid=' + $scope.schoolId)
.then(function (response) {
$scope.school.config = JSON.stringify(response.data, null, 4);
});
}
};
$scope.disable = function () {
getConfig();
$http.post('/school/' + $scope.school.id + '/update', {
deployment_id: $scope.schoolId,
component: $scope.schoolName,
is_enabled: false,
config: JSON.parse($scope.school.config)
});
};
测试用例:
describe('controller', function () {
fit('should POST payload for a scenario to disable', function () {
$scope.school.config = JSON.stringify(mockSchoolConfig, null, 4);
$scope.disable();
$scope.$digest();
$httpBackend.expect('POST', '/school/' + $scope.school.id + '/update', {
deployment_id: $scope.schoolId,
component: $scope.schoolName,
is_enabled: false,
config: $scope.school.config
}).respond(200);
//$httpBackend.flush();
});
});
Error: Unexpected request: GET /s/create-alarm/config?school_uuid=170bf60e-0153-4615-9a4e-a6bc3ad546ea
预计没有更多请求
如何测试这个函数和http请求?
【问题讨论】:
标签: angularjs unit-testing directive