I want to be able to test some inner functions that I have without putting the Javascript test code in the same file. I’ve been trying to inherit the class that has the inner functions, and then adding wrapper functions to make them public, like this:
copyPrototype(TrafficChartCanvas_test, TrafficChartCanvas);
function TrafficChartCanvas_test() {
TrafficChartCanvas_test.prototype.test_getYScale = function(reach) {
return getYScale(reach);
}
}
Assume copyPrototype successfully assigns the prototype of TrafficChartCanvas to that of TrafficChartCanvas_test. Here’s part of the file I’m attempting to test:
function TrafficChartCanvas(...) {
// some other stuff here... public functions, etc.
function getYScale(reach) {
...
}
}
However, this doesn’t work for me. Is there any other way I can do this?
getYScaleisn’t part ofTrafficChartCanvas‘s prototype, it’s within its closure. They’re two very different things.You could change the way you create
TrafficChartCanvasand restructure your code something like:This puts the function in the prototype and allows it to be tested using tests written and included in another file. You can’t keep functions private within a closure and make the accesible for external testing; you’ll have to go the prototype route.