rmaitra
rmaitra

Reputation: 41

Unavailable Module Error: Overriding an AngularJS Factory with Protractor for E2E testing

I am trying to mock a factory within one of my angularjs modules. The full angularjs application is

angular.module('administration', ['administrationServices'])

The dependency:

var module = angular.module('administrationServices')

contains a factory service:

module.factory('Example', [function(){
    return{
        list:function(){
            return ['This is real']
        }
    }
}])

This is the service I am attempting to override in my protractor e2e test. The actual test looks something like this:

describe('Example page', function(){
    beforeEach(function() {
        var mock = function(){

            // get the module and provide the mock
            var module = angular.module('administrationServices').config(['$provide', function($provide){
                $provide.factory('Example',[function(){
                    return {
                        list: function(){
                            return ['This is a Mock Test'] 
                        }
                    }
                }])
            }])
        }

        // override the mock service
        browser.addMockModule('administrationServices', mock)

    })

    it('should go to the page and see mock text', function() {
        // code that goes to page and checks if the service works
        // ...
    })

})

The issue I'm having occurs when I $ protractor conf.js, I get the error:

 Error while running module script administrationServices: [$injector:nomod] http://errors.angularjs.org/1.3.4/$injector/nomod?p0=administrationServices

This is where I'm confused. Every blog post about protractor's addMockModule uses similar syntax it seems. There are other factory services in administrationServices and those seem to get overwritten because the app can't open to the page due to those services (user and group services) not working.

Anyways, if somebody has seen this and can help direct me in the right direction, that would help; I am fairly new to mock services, protractor and e2e testing.

Upvotes: 4

Views: 430

Answers (1)

Emil
Emil

Reputation: 11

I think the problem is that your mock function does not return anything. It doesn't share the new module outside the function scope.

var mock = function(){

        // get the module and provide the mock
        return angular.module('administrationServices').config(['$provide', function($provide){
            $provide.factory('Example',[function(){
                return {
                    list: function(){
                        return ['This is a Mock Test'];
                    }
                }
            }])
        }])
    };

    // override the mock service
    browser.addMockModule('administrationServices', mock)

Just make it return the new module and it should be fine.

Upvotes: 1

Related Questions