Search code examples
angularjsmockingprotractorangularjs-e2ehttpbackend

httpBackend explicit annotation for Angular app in strict mode


I am using Protractor's addMockModule functionality to mock some request data, but am having issues with the target Angular app being in strict mode.

This is the error:

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

This is the code:

    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);      

Is there a way how I can explicitly inject $httpBackend in Angular's context here?


Solution

  • Try to explicitly provide dependencies for your run block:

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

    This is basically the idea behind strict mode, that it forces you to set injection explicitly (docs).

    Alternative approach looks like:

    angular.module('app', []).run(runBlock);
    
    runBlock.$inject = ['$httpBackend'];
    
    function runBlock($httpBackend) {
        // ...
    }