I was wondering if it was possible to test a private method of a prototype in js. I suspect it's impossible but maybe someone would surprise me...
For example, suppose I have the following class restaurant:
function Restaurant() {
}
Restaurant.prototype = (function() {
var private_stuff = function() {
// Private code here
};
return {
constructor:Restaurant,
use_restroom:function() {
private_stuff();
}
};
})();
Would it be possible to write a unit test for private_stuff method? I use jasmine for my unit tests, but I guess it doesn't really matter.
Thanks, Lior
The last time I faced something similar, I had to specifically expose private stuff, as odd as that sounds:
function Restaurant() {
}
Restaurant.prototype = (function() {
var private_stuff = function() {
// Private code here
};
var internalMethods = {};
if (window.exposeInteralMethods) {
internalMethods = {
private_stuff: private_stuff
};
}
return {
internalMethods: internalMethods,
constructor:Restaurant,
use_restroom:function() {
private_stuff();
}
};
})();
You are at risk if anyone sets window.exposeInteralMethods
besides you, but sometimes one needs 2x4 debugging of the squirmware.