protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Injecting variables when mocking module

Open ghristov opened this issue 12 years ago • 2 comments

Hey guys,

Here is the example:

 var mockedModule=function(){
     var module = angular.module('example-module').config(['$provide',function($provide){
          $provide.constant('someConstant','value');
     }])
}

browser.addMockModule('example-module',mockedModule); 

So in the tests i want to assert agans the constant's value right? Well even if i write a normal spec i must write it as a 'value'. This is duplication of course. What if i have more complicated variables, objects, services and etc.

I couldn't found way to inject shared variable when mocking module.

Refactored example:

var value = 'value'

var mockedModule=function(){
     var module = angular.module('example-module').config(['$provide',function($provide){
          $provide.constant('someConstant',value);
     }])
}

and asserting will be against the value variable itself, not the value of the variable. Duplication removed.

Maybe i'm missing something... So thank you.

ghristov avatar Feb 11 '14 11:02 ghristov

No, you're not missing something - the code that is run in the mockedModule is run in a separate context from the test. One is in the browser, one is in the node.js process running the test. So, you'll need a little duplication here. I'd say that's OK for tests.

juliemr avatar Feb 11 '14 17:02 juliemr

@juliemr Thank you very much for the answer. This clarifies a lot. :) :+1:

ghristov avatar Feb 14 '14 07:02 ghristov