I use the module template in Javascript to separate my public interface from a private implementation. To simplify what I'm doing, my code generates a diagram. The diagram consists of several parts (axis, labels, plot, legend, etc.). My code looks like this:
var Graph = function() { var private_data; function draw_legend() { ... } function draw_plot() { ... } function helper_func() { ... } ... return { add_data: function(data) { private_data = data; }, draw: function() { draw_legend() draw_plot() } } }
Some people advocate testing only the public interface of your classes, which makes sense, but I would really like to pass some tests to test each component individually. If I messed up my draw_legend () function, I would like this test to fail, not a test for the public draw () function. Am I on the wrong track here?
I could separate each of the components in different classes, for example, create a Legend class. But it seems foolish to create a class for what sometimes is only 5-10 lines of code, and that would be terrible, because I would need to pass a bunch of private state. And I could not test my helper functions. Should I do this? Should I suck it and check only the public ()? Or is there another solution?
javascript unit-testing
swampsjohn Apr 04 '09 at 0:53 2009-04-04 00:53
source share