javascript - Mocking dependent modules using Jasmine -


i have javascript modular application not using angular/backbone. trying write test case startup module facing issue.

startup.js:

    window.claimstartup = function (options) {     this.options = $.extend({}, this.options, options);     this.cmds = new this.servicemodule(this);     }       getclaimscategories: function (claimtypecode) {         var self = this;         // refresh claim categories         return $.when(self.cmds.getclaimscategories(claimtypecode))             .then(function (response) {                 return response;             });     },  

service.js:

    claimstartup.prototype.servicemodule = function (cfg) {        this.cfg = cfg;     this.init();     } 

startup.spec.js:

var claimstartup;  beforeeach(function () { spyon(window.claimstartup.prototype, 'servicemodule');   claimstartup = new window.claimstartup();  });  it("should able categories", function () { jasmine.spyon(claimstartup.cmds, 'getclaimscategories').andreturn('xyz'); var categories=  claimstartup.getclaimscategories('abc'); expect(categories).toequal('xyz'); }); 

i getting error when populating this.cmds says servicemodule constructor not defined, need mock servicemodule when claimstartup initialised.

please let me know how can write test case in scenario.

i able fix this, might useful others.

      var claimstartup;        beforeeach(function () {       var spyservice = jasmine.createspy('servicemodule');       var methodservice = window.claimstartup.prototype.servicemodule;       window.claimstartup.prototype.servicemodule = spyservice;        var spyui = jasmine.createspy('uimodule');       var methodui = window.claimstartup.prototype.uimodule;       window.claimstartup.prototype.uimodule = spyui;        claimstartup = new window.claimstartup();    }); 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -