title: nodejs module unit test method gets called in private class
body: <p>I'm trying to unit test that a method gets called in my module. The class and method are private and not exposed using <code>module.exports</code>. The modules I'm using for the tests are: <code>mocha</code>, <code>rewire</code>, <code>assert</code>, <code>sinon.spy</code>. The call I want to test is my error method, this currently throws an error, but might change later - so I don't want to test that an error is thrown, just test that <code>class.error()</code> gets called. Not sure how to procede and have tried numerous tuts online.</p>
  
  <p>The class is (currently accessed in tests using <code>rewire</code>):</p>
  
  <pre><code>var MyClass = function MyClass(o){
  
      var self = this
  
      if(!o || typeof o !== 'object')
          self.error('No configuration passed to MyClass')
  }
  
  MyClass.prototype.error = function(msg){
  
      throw Error(msg)
  }
  </code></pre>
  
  <p>My test currently, which is not working:</p>
  
  <pre><code>it('Constructs MyClass', function(done){
  
      //check constructs normally (this passes and works)
      var actual = obj.__get__("MyClass.config")
      assert.deepEqual(actual, config)
  
      /**
       * check calls error method
       */
      //stub class.error ?
      //construct class without config
      //check if class.error is called
  
      done()
  })
  </code></pre>
  
  <p>In pseudo code, what I'm hoping to do is:</p>
  
  <pre><code>var stub = stub(MyClass)
      -&gt;do('construct', null) //no config passed
      -&gt;didCall('error') //check error method is called
  </code></pre>
  
  <p>This may be a duplicate of: <a href="https://stackoverflow.com/questions/15790505/mocking-modules-in-node-js-for-unit-testing">Mocking modules in Node.js for unit testing</a>
  But it is throwing an error me: <code>Object #&lt;Object&gt; has no method 'expect'</code></p>