$ httpBackend在Protractor测试中没有响应

时间:2016-01-14 19:32:06

标签: angularjs protractor httpbackend

我正在尝试在Protractor / Jasmine中编写测试,这取决于我能否看到HTTP请求中发送的标头。为此,我正在尝试使用$ httpBackend创建一个模拟端点,该端点将响应带有标头的调用,允许我在测试中查看它们。我的代码如下:

describe('Headers', function () {
    it('should include X-XSRF-TOKEN on HTTP calls', function () {
        browser.addMockModule('httpBackendMock', function () {
            angular.module('httpBackendMock', ['CorsApp', 'ngMockE2E'])
            .run(function ($httpBackend) {
                $httpBackend.whenGET('/xsrftest')
                    .respond(function (method, url, data, headers) {
                        return headers;
                    });
            })
        });

        loginPage.get();

        browser.executeAsyncScript(function (callback) {
            var $http = angular.injector(['ng']).get('$http');

            $http.get('/xsrftest')
                .then(function (response) {
                    callback(response);
                });
        })
        .then(function (response) {
            console.log(response);
        });
    });
});

我试图遵循许多资源中规定的模式,在量角器测试中使用$ httpBackend。但是,当我运行此测试时,我得到一个Protractor超时。似乎$ http.get调用永远不会收到响应,因此永远不会调用回调,因此executeAsyncScript调用会超时。如果我对不依赖于$ http.get的回调进行虚拟调用,它会按预期工作。

在设置$ httpBackend时我做错了什么?我怎样才能让它回应我的$ http请求?

谢谢!

0 个答案:

没有答案
相关问题