What is the least-dirty way to expose private javascript vars for testing? -


i've got state machine in js (to simplify) has initial state set before happens.

define(function() {   var state = 'initial',       exports = {};    exports.getstate = function() {     return state;   };    exports.dosomething = function() {     state = 'newstate';   };    return exports; }); 

because state permanent until app reloaded, after first test state never 'initial' again, , need way reset it.

which least dirty way of doing this? should i...

a) make state public mark private _?

define(function() {   var exports = {};    exports._state = 'initial'    exports.getstate = function() {     return this.state;   };    exports.dosomething = function() {     this.state = 'newstate';   };    return exports; }); 

b) make state writable via function?

define(function() {   var state = 'initial',       exports = {};    exports.getstate = function() {     return state;   };    exports.dosomething = function() {     state = 'newstate';   };    if(window.xxtests) {     window.xxtests.module = {       setstate: function(newstate) {         state = newstate;       }     };   }    return exports; }); 

(where xx app prefix , xxtests defined part of test runner)

or

c) else entirely didn't think of?

your thoughts , suggestions appreciated.

general answer: better way of testing component without exposing private data putting test code inside component. of course becomes quite dirty if language doesn't support in clean way (like d does).

in case suggest second way (i mean option b), because doesn't expose private data unless explicitly needed (only during tests):

if(youaredoingatest) {     exports.reset=function() { state='initial'; }; } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -