httpBackend严格模式下Angular应用程序的显式注释

时间:2015-08-13 08:27:35

标签: angularjs mocking protractor angularjs-e2e httpbackend

我正在使用Protractor的addMockModule功能来模拟一些请求数据,但是目标Angular应用程序处于严格模式时遇到了问题。

这是错误:

Failed: unknown error: [$injector:strictdi] function($httpBackend) is not using explicit annotation and cannot be invoked in strict mode

这是代码:

    var httpBackendMock = function() {
        angular.module('httpBackendMock', ['my-app', 'ngMockE2E'])
            .run(function($httpBackend){
                var expected_response = {"limit": 1}

            $httpBackend.whenGET(/homepage/).respond(function() {
                return [200, expected_response];
            });
            $httpBackend.whenGET(/.*/).passThrough();
        });
    };

    browser.addMockModule('httpBackendMock', httpBackendMock);      

有没有办法在Angular的上下文中明确注入$ httpBackend?

1 个答案:

答案 0 :(得分:1)

尝试显式提供运行块的依赖项:

.run(['$httpBackend', function($httpBackend) {
    //     ^^^ - inject explicitly
}]);

这基本上是严格模式背后的想法,它迫使你明确设置注入(docs)。

替代方法如下:

angular.module('app', []).run(runBlock);

runBlock.$inject = ['$httpBackend'];

function runBlock($httpBackend) {
    // ...
}