unit-testing functions in module
<p>Ok, I'm making a serious stab at learning about unit testing for the first time, I'm using Mocha &amp; Sinon.</p>

<p>So hypothetical dumb module, contains functions <code>foo</code> and <code>bar</code>.  <code>Foo</code> depends on <code>bar</code>.</p>

<pre><code>(function() {

  var bar = function(callback) {
    if (willOfTheGods) {
      callback('err', null);
    } else
      callback(null, 'hurrah');
  }

  var foo = function() {

    bar(function(err, res) {
      //blah deh blah
    });
  }

  module.exports = {
    bar: bar,
    foo: foo
  }

})();
</code></pre>

<p>I want to stub <code>bar</code> so I can test <code>foo</code>.</p>

<p>This seems logical from the Sinon docs ...</p>

<pre><code>var myModule = require('../myModule');
sinon.stub(myModule, 'bar')
myModule.bar.callsArgWith(0, ['err', null]);
</code></pre>

<p>but it only works if I call <code>bar</code> in the test itself.</p>

<pre><code>myModule.bar(function(err, result){
  console.log('err, result');
});
</code></pre>

<p>When I call <code>myModule.foo</code> it still uses the original 'unstubbed one'.</p>

<p>It seems like creating a stub just creates a new local variable for that 
function rather than stubbing it in the module.</p>

<p>Or am I missing something completely - or just doing the wrong thing?</p>