Angularjs 角度单元测试:错误:意外请求:GET

Angularjs 角度单元测试:错误:意外请求:GET,angularjs,unit-testing,karma-jasmine,Angularjs,Unit Testing,Karma Jasmine,如何在下面测试这种类型的http格式 $http({ method: 'GET', url: 'https://api.github.com/user/repos', headers:{ 'Authorization': "Basic " + btoa("xxxx:xxxx"), 'Accept': 'application/json; odata=verbose' } }) .success(function(data, stat

如何在下面测试这种类型的http格式

$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' });

}));
我得到这个错误

错误:意外请求:获取否 需要更多请求

有什么想法吗?

你能试试这个吗

$httpBackend.whenGET('https://api.github.com/user/repos', undefined, {
    Authorization: "Basic " + btoa("xxxxx:xxxx"),
    Accept: "application/json;odata=verbose"
}).respond(function(){ return [200,[{foo: 'bar' }]]});

不应该。当“得到”时https://api.github.com/user/repos“在实际通话之前吗?为什么?我遵循这个指南,$http调用的捕获应该在实际调用之前定义,在$httpBackend之前移动$httpBackend,但没有区别。@teelou,基本上$httpBackend的respond方法可以使用两个版本一个是1函数返回一个状态数组,数据和标题信息等,第二个是包含状态和数据等的静态数据集。尽管在功能版本中,状态是强制性的。