Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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

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:

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)
like image 980
jonnie Avatar asked Feb 10 '14 11:02

jonnie


1 Answers

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

});
like image 188
jonnie Avatar answered Oct 13 '22 15:10

jonnie