Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are still missing the point. </p> <p>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. </p> <p>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.</p> <p>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. </p> <pre><code>var Raphael= function(listIterator) { listIterator.method(); }(new ListIterator()); </code></pre> <p>Spurious hacks, such as shown below, are totally inappropriate (in unit tests or anywhere). </p> <p>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.</p> <p>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.</p> <p>Regarding the code you posted, you forgot <code>var</code>, missed a semicolon, and used two reserved words as identifiers: <code>private</code> and <code>public</code>. </p> <p>The consequence of not using <code>var</code> is the potential to trigger errors and problems related to various implementations of nonstandard <code>GlobalScopePolluter</code>-type objects ("Object doesn't support this property or method" seen in IE). The consequence of using a FutureReservedWord is <code>SyntaxError</code>. Implementation may provide a syntax extesion to <i>allow</i> 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.</p> <p>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. </p> <pre><code>// 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)); </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload