In many frameworks, internal function variables are used as private variables, for example
Raphael = (function(){
var _private = function(a,b) {return a+b;};
var _public = function(a) {return _private(a,a);}
var object = {mult2:_public};
return object;
})();
here, we cannot access from the global namespace the variable named private
, as it is an inner variable of the anonymous function in the first line.
Sometimes this function is contains a big Javascript framework, so that it wouldn't pollute the global namespace.
I need to unit tests some object Raphael
uses internally (in the above example, I wish to run unit tests on the object private
). How can I test them?
edit: I received comments about unit tests which are supposed to test public interfaces.
Let me specify a use case. I'm writing a library called Raphael
. This library is supposed to add only a single name to the global namespace, and nothing more. This is a peculiar requirement for Javascript, since Javascript does not have namespaces.
Let's say Raphael
uses a linked list. If Javascript had the notion of packages, I would do
require 'linked_list'
Raphael = (function(){/* use linked list */})();
However Javascript does not allow me to do that in any way that wouldn't pollute the global scope with the linked list object! I'm therefore bound to inline linked_list
into Raphael's local scope:
Raphael = (function(){
/* implement linked list */
var linked_list = function(){/*implementation*/};
})();
And now I want to test linked_list
implementation.