【问题标题】:Unavailable Module Error: Overriding an AngularJS Factory with Protractor for E2E testing不可用的模块错误:使用量角器覆盖 AngularJS 工厂以进行 E2E 测试
【发布时间】:2015-09-01 00:42:50
【问题描述】:

我试图在我的一个 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
        // ...
    })

})

当我$ protractor conf.js 时出现问题,我收到错误消息:

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

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

无论如何,如果有人看到了这一点并且可以帮助我朝着正确的方向前进,那将会有所帮助;我对模拟服务、量角器和 e2e 测试相当陌生。

【问题讨论】:

    标签: angularjs protractor e2e-testing


    【解决方案1】:

    我认为问题在于您的模拟函数没有返回任何内容。它不共享函数范围之外的新模块。

    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)
    

    只要让它返回新模块就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      相关资源
      最近更新 更多