0

I want to be able to define a variable and then use said variable in the respond() method inside the module.run() function. I have this code inside my it():

    var testValue=randomValue(); // suffice to say, a random value generator
    var httpBackendMock = function() {
        angular.module('httpBackendMock', ['ngMockE2E', 'name.of.app'])
            .run(function($httpBackend) {
                $httpBackend.whenPOST(/.*\/api\/data/).respond(function(method, url, data, headers) {
                    return [200, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> \
                    <data>'+testValue+'</data>', {}];
                });
....

But the testValue variable defined above the httpBackendMock object isn't visible inside the run() function at all, but will be 'undefined'.

My understanding is that 'var' defined variables in JS are available to inner-scoped code, but that's not happening here. Is there some way to get variables to work inside there?

Keith Tyler
  • 719
  • 4
  • 18
  • 1
    `httpBackendMock` isn't an object. It is a function. Please, show what happens with it. If it is executed with `browser.executeScript(httpBackendMock)`, this obviously won't work, because outer function scope runs in Node and inner function scope runs in browser. – Estus Flask Jul 29 '16 at 23:36
  • @estus It is from https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend . I tagged it with ngmocke2e because I probably need people who are familiar with that framework. – Keith Tyler Jul 31 '16 at 18:00
  • There's no mention of `httpBackendMock` function in the link you've posted. Please, show how this function is being called. – Estus Flask Jul 31 '16 at 18:22
  • `browser.addMockModule('httpBackendMock', httpBackendMock);` Example from http://product.moveline.com/testing-angular-apps-end-to-end-with-protractor.html#beyond-the-basics – Keith Tyler Aug 01 '16 at 15:57

2 Answers2

1

This piece of code

var testValue=randomValue();

is executed in Node. And this piece of code

function() {
    angular.module('httpBackendMock', ['ngMockE2E', 'name.of.app'])
    ...
}

is converted to string, passed to client side and executed in browser.

Outer function scope is not available in inner function, this explains why testValue is undefined in httpBackendMock function, and it will throw an error if httpBackendMock function uses strict mode.

Additional data can be passed through executeScript and addMockModule extra arguments, which will be available on client side.

As it is shown in documentation, the arguments after the first two are passed to the browser.

It should be something like

var httpBackendMock = function(testValue) {
    angular.module('httpBackendMock', ['ngMockE2E', 'name.of.app'])
    ...
}
...
var testValue=randomValue();
browser.addMockModule('httpBackendMock', httpBackendMock, testValue);
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
0

The answer is to pass in a nested array of values and add an argument to the function signature of the mockmodule object.

var httpBackendMock = function(args) {
    var isFoo=args[0];
    var isBar=args[1];
...
browser.addMockModule('httpBackendMock', httpBackendMock, [['foo','bar']]);

Found the answer at: Protractor addMockModule additional arguments not working?

Community
  • 1
  • 1
Keith Tyler
  • 719
  • 4
  • 18