I just read this great book on Test-Driven Development for Javascript, and in there he mentioned an amazing way of creating objects with private members, originally created by Douglas Crockford, that is called “functional inheritance”. It goes like this:
this.funcInh = function() {
var privateString = "functional inheritance";
function setPrivateString(string) {
privateString = string;
}
function getPrivateString() {
return privateString;
}
return {
setPrivateString: setPrivateString,
getPrivateString: getPrivateString
};
};
While i really love this way of creating objects, I’m wondering how on earth i can test it, other than testing the return value of the privileged functions ‘setPrivateString’ and ‘getPrivateString’. This is just an example, but I can’t really see any way to test “private” functions or that the privileged functions call the functions they should… Any ideas?
Do you really want to test privates?
I think as far as your public methods are working as expected and providing correct results to “outside world” you should not care what and how it works “inside the box”.
See more on this: Should I test private methods or only public ones?