角度单位测试:错误:意外请求:获取

时间:2016-01-22 15:30:00

标签: 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, 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' });

}));

我收到此错误,

  

错误:意外请求:获取https://api.github.com/user/repos否   更多要求

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

你可以试试吗

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.lightcurb.example" >

<!-- SDK feature -->
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />

<application
android:name=".LCExampleApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >