不可用的模块错误:使用Protractor覆盖AngularJS Factory以进行E2E测试

我试图在我的一个 angularjs模块中模拟一个工厂.完整的angularjs应用程序是

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

依赖:

var module = angular.module('administrationServices')

包含工厂服务:

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

这是我试图在量角器e2e测试中覆盖的服务.实际测试看起来像这样:

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
        // ...
    })

})

当我$proractor conf.js时,我遇到了这个问题,我得到了错误:

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

这是我很困惑的地方.关于量角器的addMockModule的每篇博文都使用了类似的语法.在administrationServices中还有其他工厂服务,这些服务似乎被覆盖,因为由于这些服务(用户和组服务)不起作用,应用程序无法打开页面.

无论如何,如果有人看到这个并且可以帮助我指导正确的方向,那将有所帮助;我是模拟服务,量角器和e2e测试的新手.

解决方法

我认为问题是你的mock函数没有返回任何东西.它不在功能范围之外共享新模块.

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)

只是让它返回新模块,它应该没问题.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...