为什么我得到“TypeError:无法读取财产'恢复未定义”当与sinon.js短语mongoose

我试图让我的脑袋周围,并与Sinon.js嘲笑mongoose和我得到的错误: TypeError: Cannot read property 'restore' of undefined 。 我曾尝试search谷歌和SO,但一直没有运气。 有人可以告诉我,我正在接近这个,如果是的话,我做错了,这个错误被抛出。 如果我这样做是错误的,我会赞赏一个正确的方向

这是我的模型/架构:

 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 }; } 

这是我使用Mocha&chai进行testing的规范:

 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(); }); }); }); 

这是我的堆栈错误:

  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.<anonymous> (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) 

       

网上收集的解决方案 "为什么我得到“TypeError:无法读取财产'恢复未定义”当与sinon.js短语mongoose"

问题是,我试图按照我在网上find的很多片段来指导原型,所以我改变了

 var stub = sinon.stub(PeopleProvider.Model.prototype, 'find', somefunction); 

 var stub = sinon.stub(PeopleProvider.Model, 'find', somefunction); 

我也意识到修复这个错误后,我也错过了我的callback,所以这里是testing的全部变化:

 it("Test: stubbing find for getAllPeople", function (done) { var stub = sinon.stub(PeopleProvider.model, 'find', function (callback) { var results = [{ name: "John", addresses: ["abc", "123", "xyz"] }, { name: "Jane", addresses: ["123 fake st", "somewhereville"] }, { name: "Joe", addresses: ["someplace", "overthere", "here"] }]; callback(null, 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("123 fake st"); expect(data[2].name).to.equal("Joe"); expect(data[2].addresses.length).to.equal(3); expect(data[2].addresses[0]).to.equal("someplace"); done(); }); });