angularjs - Reusing angular mocks in Jasmine tests using $provide -


i wish reuse mocks instead of having set them in every unit test has them dependency. i'm having hard time figuring out how inject them properly.

here's attempt @ unit test setup, of course fails because configservicemockprovider doesn't exist.

describe('loginservice tests', function () {    var loginservice;    beforeeach(module('mocks'));    beforeeach(module('services.loginservice', function ($provide, _configservicemock_) {     $provide.value("configservice", _configservicemock_);      /* instead of having type e.g. everywhere configservice used     *  $provide.value("configservice", { 'foobar': function(){} });     */   });    beforeeach(inject(function (_loginservice_) {     loginservice = _loginservice_;   }); } 

configservicemock

angular.module('mocks').service('configservicemock', function() {   this.init = function(){};   this.getvalue = function(){}; } 

i realize have configservicemock.js make global window object, , thereby not needing load this. feel there should better way.

try this:

describe('using externally defined mock', function() {   var configservicemock;    beforeeach(module('mocks'));    beforeeach(module('services.configservice', function($provide) {     $provide.factory('configservice', function() {return configservicemock;});   }));    beforeeach(module('services.loginservice'));    beforeeach(inject(function (_configservicemock_) {     configservicemock = _configservicemock_;   }));    // not combine call 1 above   beforeeach(inject(function (_loginservice_) {     loginservice = _loginservice_;   }));    it('should have been given mock', function() {     expect(configservicemock).tobedefined('the mock should have been defined');     expect(loginservice.injectedservice).tobedefined('something should have been injected');     expect(loginservice.injectedservice).tobe(configservicemock, 'the thing injected should mock');   }); }); 

according this answer, have put of calls module before of calls inject.

this introduces bit of catch-22 because have have reference configservicemock (via inject) spec before can set on loginservice (done in module call)

the work-around set angular factory function configservice dependency. cause angular lazy load service, , time have received reference configservicemock.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -