如何测试这种类型的http格式?角单元测试:错误:意外的请求:GET
$http({
method: 'GET',
url: 'https://api.github.com/user/repos',
headers:{
'Authorization': "Basic " + btoa("xxxx:xxxx"),
'Accept': 'application/json; odata=verbose'
}
})
.success(function(data, status, headers, config) {
$scope.valid = true;
$scope.collection = data;
})
.error(function(data, status, headers, config) {
$scope.error = data;
});
测试代码,
it('should demonstrate using when (200 status)', inject(function($http, $httpBackend) {
var $scope = {};
/* Code Under Test */
$http({
method: 'GET',
url: 'https://api.github.com/user/repos',
headers:{
'Authorization': "Basic " + btoa("xxxxx:xxxx"),
'Accept': 'application/json; odata=verbose'
}
})
.success(function(data, status, headers, config) {
$scope.valid = true;
$scope.collection = data;
})
.error(function(data, status, headers, config) {
$scope.error = data;
});
/* End */
$httpBackend
.when('GET', 'https://api.github.com/user/repos', undefined, {
Authorization: "Basic " + btoa("xxxxx:xxxx"),
Accept: "application/json;odata=verbose"
})
.respond(200, { foo: 'bar' });
$httpBackend.flush();
expect($scope.valid).toBe(true);
expect($scope.collection).toEqual({ foo: 'bar' });
}));
我得到这个错误,
Error: Unexpected request: GET https://api.github.com/user/repos No more request expected
任何想法?
应该不是'。当( 'GET',“HTTPS:// api.github.com /用户/ repos''是实际工作呼叫之前 – maurycy
如何来了吗?我下面前应定义本指南http://www.bradoncode.com/blog/2015/06/26/unit-testing-http-ngmock-fundamentals/ – laukok
美中不足的'$ http'电话实际调用时,将'$ httpBackend'之前'$ HTTP()' – maurycy