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.
A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function's variables — a scope chain. The closure has three scope chains: it has access to its own scope — variables defined between its curly brackets. it has access to the outer function's variables.
Yes. This method is called currying.
You are still missing the point.
The point of unit testing is to verify that the object's public interface does what is expected of it. Unit tests show how the code works.
The only thing that should be tested is the object's public interface. That way, when you the developer want to change how the object is implemented, you only worry about if the object being tested still does what it is expected of it.
If you feel that the object that is inside that closure needs testing, then test that, but do it externally and then pass it into the closure.
var Raphael= function(listIterator) {
listIterator.method();
}(new ListIterator());
Spurious hacks, such as shown below, are totally inappropriate (in unit tests or anywhere).
Test functions should be simple, test one thing only, and have one assertion. This can usually happen in three to ten lines of test code.
When you get to the point where your test functions are complicated, as they would be following the approach you are asking about, then either (1) realize that your design might not be what you want it to be and change it so that it is, or (2) change your expectations in the test.
Regarding the code you posted, you forgot var
, missed a semicolon, and used two reserved words as identifiers: private
and public
.
The consequence of not using var
is the potential to trigger errors and problems related to various implementations of nonstandard GlobalScopePolluter
-type objects ("Object doesn't support this property or method" seen in IE). The consequence of using a FutureReservedWord is SyntaxError
. Implementation may provide a syntax extesion to allow FutureReservedWord as identifier, and indeed many do, however it is best to not rely on such extensions and if you got an error, it would be completely your fault.
You mentioned delivering code to users. I suggest that you not do that until you get some more experience and understanding with what you are doing.
// DO NOT USE THIS CODE.
var Raphael = (function(){
var _private = function(a,b) {return a+b;};
var _public = function(a) {return _private(a,a);};
var object = {mult2:_public};
return object;
})();
var leakedFunction;
// Spurious hack:
// Give valueOf a side effect of leaking function.
// valueOf is called by the _private function as a
// side effect of primitive conversion, where
// ToPrimitive(input argument, hint Number) results
// in calling valueOf.
function valueOfSnoop(){
leakedFunction = leakedFunction || valueOfSnoop.caller || function(){};
return 2;
}
var a = {
valueOf : valueOfSnoop
};
Raphael.mult2(a, 3);
var privateMathod = leakedFunction;
alert(leakedFunction(1, 2));
That example code is only as a demonstration that such thing is possible. Given the choice, it is a poor alternative to the alternatives mentioned earlier; either change your design or change your tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With