繁体   English   中英

AngularJS $ httpBackend ExpectGET无法正常工作

[英]AngularJS $httpBackend expectGET not working

我正在为AngularJS工厂编写QUnit测试。 这是工厂代码:

var app = angular.module('App', []);
app.factory('$groupFactory', function($rootScope, $http) {
    return {
        'getAll': function(_callback) {
            $http.get("get/values/from/server", {
                headers: {
                    'Content-type': 'application/json'
                }
            }).success(function(data, status, headers, config) {
                _callback(data);
            }).
            error(function(data, status, headers, config) {
                _callback(data);
            });
        },
    }
});

另请参阅以下Qunit测试案例。 test-1从$httpBackend工程获得http响应,但在test-2中却没有。

var $scope,
    $rootScope,
    $http,
    $httpBackend,
    $groupFactory,
    injector = angular.injector(['ng', 'App', 'ngMockE2E']),
    init;
init = {
    setup: function() {
        $rootScope = injector.get('$rootScope').$new();
        $groupFactory = injector.get('$groupFactory');
        $httpBackend = injector.get('$httpBackend');
        $httpBackend
            .when('GET', "get/values/from/server")
            .respond({'response': 'success'});
    }
};

module('$groupFactory', init);

// test-1
test("getAll", function() {
    expect(1);
    $groupFactory.getAll(function(data) {
        equal(data.response, 'success', "success casse");
        start();
    });
    stop();
});

// test-2
test("getAll", function() {
    expect(1);
    $httpBackend.expectGET("get/values/from/server").respond(404, {
        response: 'failure'
    });
    $groupFactory.getAll(function(data) {
        equal(data.response, 'failure', "failure casse");
        start();
    });
    stop();
});

知道为什么它不起作用吗?

这是jsFiddle演示

$httpBackend.flush()之后调用$httpBackend.flush() stop()将起作用:

stop();
$httpBackend.flush();

这是更新的演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM