White-box testing in Javascript - how to deal with privacy?
Posted
by Max Shawabkeh
on Stack Overflow
See other posts from Stack Overflow
or by Max Shawabkeh
Published on 2010-05-28T22:30:55Z
Indexed on
2010/05/28
22:32 UTC
Read the original article
Hit count: 187
I'm writing unit tests for a module in a small Javascript application. In order to keep the interface clean, some of the implementation details are closed over by an anonymous function (the usual JS pattern for privacy). However, while testing I need to access/mock/verify the private parts.
Most of the tests I've written previously have been in Python, where there are no real private variables (members, identifiers, whatever you want to call them). One simply suggests privacy via a leading underscore for the users, and freely ignores it while testing the code. In statically typed OO languages I suppose one could make private members accessible to tests by converting them to be protected and subclassing the object to be tested. In Javascript, the latter doesn't apply, while the former seems like bad practice.
I could always wall back to black box testing and simply check the final results. It's the simplest and cleanest approach, but unfortunately not really detailed enough for my needs.
So, is there a standard way of keeping variables private while still retaining some backdoors for testing in Javascript?
© Stack Overflow or respective owner