jeudi 4 août 2016

Testing mongoose with Chai and Sinon

I want to test if a function inside a module uses the mongoose save method correctly. With a similar test of the same behavior, I was able to get the correct result. My connection to the database is active when I am running the test. Is it that a need to mock the database in the test ?

var expect = require("chai").expect;
var sinon = require('sinon');

var toTest = require('../toTest');
var mongoose = require('mongoose');

var Model = require('../model').Model;


describe("Users", function() {
    describe("add function", function() {
          it("should call the mongoose.save function once", function() {
            var spy;

             spy = mongoose.save = sinon.spy();

              var model = new Model({
                 name: 'name',
                 type: 'type
            });

            toTest.add(model);
            expect(spy.calledOnce).to.equal(true);
        });
     });
 });

Aucun commentaire:

Enregistrer un commentaire