title: Why Am I getting "TypeError: Cannot read property 'restore' of undefined" when stubbing mongoose with sinon.js
body: <p>I am trying to get my head around stubbing and mocking mongoose with Sinon.js and I am getting the error : <code>TypeError: Cannot read property 'restore' of undefined</code>. I have tried Searching Google and SO but have had no luck at all. Could someone please tell me Am I approaching this correctly and if so What am I doing wrong that this error is being thrown. If I am going about this all wrong I would appreciate a point in the right direction</p>
  
  <p>This is my Model/Schema:</p>
  
  <pre><code>var Mongoose = require("mongoose");
  var Schema = Mongoose.Schema;
  
  exports = module.exports = function (host, database, port) {
  
      var mySchema = new Schema({
          name: {
              type: String,
              required: true,
              unique: true
          },
          addresses: {
              type: Array,
              required: false
          }
      }, {
          strict: true
      });
      mySchema.path('name').index({
          unique: true
      });
  
      var db = Mongoose.createConnection(host, database);
      var model = db.model('people', mySchema);
      var getAllPeople = function (callback) {
  
          var error = null,
              data = null;
  
  
          return model.find(function (err, people) {
              if (!err) {
                  data = people;
              } else {
                  error = err;
              }
              callback(error, people);
          });
      };
  
      return {
          Model: model,
          getAllPeople: getAllPeople
  
      };
  
  }
  </code></pre>
  
  <p>and this is my Spec for testing using Mocha &amp; chai:</p>
  
  <pre><code>var expect = require("chai").expect;
  var sinon = require("sinon");
  var PeopleProvider = require("../models/mySchema.js")("localhost", "test_db");
  describe("Unit tests with sinon", function () {
  
      it("Test: stubbing find for getAllPeople", function (done) {
  
          var stub = sinon.stub(PeopleProvider.Model.prototype, 'find', function () {
              return [{
                  name: "John",
                  addresses: ["abc", "123", "xyz"]
              }, {
                  name: "Jane",
                  addresses: ["123 fake st", "somewhereville"]
              }, {
                  name: "Joe",
                  addresses: ["someplace", "overthere", "here"]
              }];
          }); 
  
          results = PeopleProvider.getAllPeople(function (error, data) {
  
  
              expect(data instanceof Array).to.equal(true);
              expect(data.length).to.equal(3);
              expect(data[0].name).to.equal("John");
              expect(data[0].addresses.length).to.equal(3);
              expect(data[0].addresses[0]).to.equal("abc");
  
              expect(data[1].name).to.equal("Jane");
              expect(data[1].addresses.length).to.equal(2);
              expect(data[1].addresses[0]).to.equal("somewhereville");
  
              expect(data[2].name).to.equal("Joe");
              expect(data[2].addresses.length).to.equal(3);
              expect(data[2].addresses[0]).to.equal("someplace");
  
              done();
          });
  
      });
  });
  </code></pre>
  
  <p>this is my stack error:</p>
  
  <pre><code>  1) Unit tests with sinon Test: stubbing find for getAllPeople:
       TypeError: Cannot read property 'restore' of undefined
        at Object.wrapMethod (C:\~censored~\node_modules\sinon\lib\sinon.js:74:30)
        at Object.stub (C:\~censored~\node_modules\sinon\lib\sinon\stub.js:56:22)
        at Context.&lt;anonymous&gt; (C:\~censored~\test\myModelSpec.js:8:22)
        at Test.Runnable.run (C:\Users\~censored~\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:204:15)
        at Runner.runTest (C:\Users\~censored~\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:374:10)
        at C:\Users\~censored~\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:452:12
        at next (C:\Users\~censored~\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:299:14)
        at C:\Users\~censored~\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:309:7
        at next (C:\Users\~censored~\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:247:23)
        at Object._onImmediate (C:\Users\~censored~\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:276:5)
        at processImmediate [as _immediateCallback] (timers.js:330:15)
  </code></pre>