title: How can i use sinon to stub functions for neo4j Thingdom module
body: <p>I am having some issues writing some unit tests where i would like to stub out the functionality of the neo4j Thingdom module.</p>
  
  <p>After a few hours of failed attempts i have been searching around the web and the only point of reference i found was a sample project which used to sinon.createStubInstance(neo4j.GraphDatabase); to stub out the entire object.  For me, and becuase this seemed to a be a throw away project i wanted a more fine grained approach so i can test that for instance as the Thingdom API outlines when saving a node you create it (non persisted) persist it and then you can index it if you wish which are three calls and could be outlined in multiple specific tests, which i am not sure can be achieved with the createStubInstance setup (i.e. found out if a function was called once).</p>
  
  <p>Example "create node" function (this is just to illustrate the function, i am trying to build it out using the tests)</p>
  
  <pre><code>User.create = function(data, next){
      var node = db.createNode(data);
  
      node.save(function(err, node){
          next(null,node);
      });
  };
  </code></pre>
  
  <p>I am able to stub functions of the top level object (neo4j.GraphDatabase) so this works:</p>
  
  <pre><code>    it('should create a node for persistence', function(){
          var stub = sinon.stub(neo4j.GraphDatabase.prototype, 'createNode');
  
          User.create({}, res);
  
          stub.calledOnce.should.be.ok;
          stub.restore();
      });   
  </code></pre>
  
  <p>The issue comes with the next set of test i wish to run which tests if the call to persist the node to the database is called (the node,save) method:</p>
  
  <p>I am not sure if this is possible or it can be achieved but i have tried several variations of the stub and non seem to work (on neo4j.Node, neo4j.Node.prototype) and they all come back with varying errors such as can't wrap undefined etc. and this is probably due to the createNode function generating the node and not my code directly.  </p>
  
  <p>Is there something i am glaringly doing wrong, am i missing the trick or can you just not do this?  if not what are the best tactics to deal with stuff like this?</p>