Protractor addMockModule additional arguments not working?

自作多情 提交于 2019-12-22 18:32:50

问题


This seems so, so easy, but I can't figure out why this simple code doesn't work.

I am adding a mock module to mock my API backend in my Angular E2E tests. I'm using Protractor 1.6.0. I need to pass additional arguments to the mocked module, which, according to the Protractor docs, is possible by just sending them as additional arguments. However, my function claims it has no arguments...

var mock = function() {
  // This is undefined and arguments.length is 0....why???
  var env = arguments[0];
  var mocks = angular.module('mocks.login', ['MyApp', 'ngMockE2E']);
  mocks.run(function($httpBackend) {
    $httpBackend.whenGET(env.apiBase + '/companies').respond([]);
  });
};

browser.addMockModule('mocks.login', mock, {apiBase: ""});

If it matters, I'm doing this in my Protractor config file in an onPrepare, as I'm trying to mock the API calls used during a user's login. Any help would be much appreciated.


回答1:


Appearantely the args should be passed as array. This is how it worked for me:

        var test =  {
            val1: 'val1',
            val2: 'val2'
        };
       browser.addMockModule('mockModule', function( args) {
            angular.module('mockModule', [])
                .value('TEST', args[0]);

        }, [test]);



回答2:


This works for me:

var mock = function(args) {
    // args.apiBase should be available here
    var env = args;
    var mocks = angular.module('mocks.login', ['MyApp', 'ngMockE2E']);
    mocks.run(function($httpBackend) {
        $httpBackend.whenGET(env.apiBase + '/companies').respond([]);
    });
};

browser.addMockModule('mocks.login', mock, {apiBase: ""});


来源:https://stackoverflow.com/questions/27883577/protractor-addmockmodule-additional-arguments-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!