javascript - Handling constructor related functions while testing with mocha and sinon -


i testing application in declare specific variable within constructor , assign return of function imported class.
how code looks like:

var someclass;  function constructor() {    this.someclasshandler = null; }  constructor.prototype.newsomeclass = function() {    someclass = require('./someclass.js');     return new someclass(); }  constructor.prototype.getfunction = function() {    this.someclasshandler = this.newsomeclass(); }  constructor.prototype.callsomefunction = function() {    this.someclasshandler.somefunction(); } 

when try test code whether function 'somefunction' imported class 'someclass' called within method'callsomefunction' receive failed test responses variable 'someclasshandler' still equals null (which makes sense not call method 'getfunction' assigns class variable).

i use mocha , sinon tests, bit helpless whether mock or stub class in order assign variable 'this.someclasshandler' correctly.

the main purpose of test regarding issue should check whether function 'somefunction' called in method 'callsomefunction'.

this bit how should like, not make sense (serves purpose understand intend test):

var myconstructor = new constructor(); var mysomeclass = new someclass(); sinon.stub(myconstructor, 'newsomeclass').returns(mysomeclass); var mysomeclassmock = sinon.mock(mysomeclass);  mysomeclassmock.expects('somefunction').once(); myconstructor.callsomefunction();  mysomeclassmock.verify(); 

i think feasible call method 'getfunction' before execute 'callsomefunction' method, great find solution problem without calling method.

after searching while possible solution still unable fix problem great receive in order fix issue.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -