简体   繁体   English

为什么我得到“TypeError:无法读取未定义的属性'恢复'当使用sinon.js删除mongoose时

[英]Why Am I getting “TypeError: Cannot read property 'restore' of undefined” when stubbing mongoose with sinon.js

I am trying to get my head around stubbing and mocking mongoose with Sinon.js and I am getting the error : TypeError: Cannot read property 'restore' of undefined . 我试图通过Sinon.js来解释和模拟mongoose并且我得到错误: TypeError: Cannot read property 'restore' of undefined I have tried Searching Google and SO but have had no luck at all. 我曾尝试搜索Google和SO,但根本没有运气。 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 如果我认为这一切都错了,我会欣赏正确方向的一点

This is my Model/Schema: 这是我的模型/架构:

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

    };

}

and this is my Spec for testing using Mocha & chai: 这是我使用Mocha&chai进行测试的规范:

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

    });
});

this is my stack error: 这是我的堆栈错误:

  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)

The Problem was that I was trying to stub the Prototype as directed by alot of snippets I found online so I changed 问题是我试图按照我在网上发现的很多片段的指示来保留原型,所以我改变了

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

to

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

I also realized after fixing this error that I also was missing my callback so here is the full changes for the test: 在修复此错误后我也意识到我也错过了我的回调,所以这里是测试的完整更改:

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

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 为什么我收到“Select.js:1555 Uncaught TypeError:无法读取未定义的属性‘isSelectOptGroup’” - Why I am getting "Select.js:1555 Uncaught TypeError: Cannot read property 'isSelectOptGroup' of undefined" React - sinon 测试 - 获取 TypeError:无法读取未定义的属性“子字符串” - React - sinon testing - getting TypeError: Cannot read property 'substring' of undefined 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 我应该如何避免使用Sinon.js存根属性 - How should I avoid stubbing properties with Sinon.js 为什么我收到此错误:未捕获的TypeError:无法读取undefined的属性&#39;john&#39; - Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined 为什么我收到错误:类型错误:无法读取未定义的属性“地图”? - Why am I getting an error : TypeError: Cannot read property 'map' of undefined? 为什么我收到错误“UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”? - Why am I getting the error “UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”? 为什么我收到 TypeError:无法读取未定义的属性“执行” - Why am I receiving TypeError: cannot read property 'execute' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM