AngularJs UnitTesting $ httpBackend不提供待处理请求

时间:2017-03-11 20:24:09

标签: angularjs karma-jasmine

这是我的测试代码,并始终收到上面的错误消息。

'use strict';
describe('getmodules', function () {
window.__env.platform = 'jasmine'; // web || mobile || desktop
beforeEach(function () { module('myApp', 'ngMockE2E', 'ngCookies'); });
var service, $httpBackend, defaultAlertFactory, $cookies, $http;
var $controller;
var $rootScope;

beforeEach(inject(function ($injector) {
    service = $injector.get('dashboardService');
    $httpBackend = $injector.get('$httpBackend');
    $rootScope = $injector.get('$rootScope');
    $http = $injector.get('$http');
}));

it("should return a list of getmodules", function () {
    debugger;
    var result1 = null;

    $httpBackend.when('GET', '/home/modulesInfo').respond(true, ['david', 'James', 'Sam']);

    /* Code Under Test */
    $http({
        method: 'GET',
        url: '/home/modulesInfo'
    }).then(function (success,response) {
        result1 =response;
    }, function (error) {

    });
    $httpBackend.flush();
    expect(result1).toEqual(["david", "James", "Sam"]);

});

});

=============================================== ========= 不知道为什么?有$ http.get(),并使用flush()来调用fack httpBackend调用,应该不错。仍然没有工作

1 个答案:

答案 0 :(得分:0)

您需要更改您的可用函数语法,如下所示:

并查看此工作Plunker

?unzip