我正试图用Sinon.js来解释和模仿mongoose,我收到了错误:TypeError: Cannot read property 'restore' of undefined
。我曾尝试搜索Google和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& amp;测试的规范。柴:
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)
答案 0 :(得分:3)
问题是我试图按照我在网上发现的很多片段的指示来保留原型,所以我改变了
var stub = sinon.stub(PeopleProvider.Model.prototype, 'find', somefunction);
到
var stub = sinon.stub(PeopleProvider.Model, 'find', somefunction);
我在修复此错误后也意识到我也错过了我的回调,所以这里是测试的完整更改:
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();
});
});