Last active
August 15, 2017 22:00
-
-
Save ppg/808697fdb740c284aaafc681fbfd373f to your computer and use it in GitHub Desktop.
Demonstration of deep matching issues with sinon-chain calledWith
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const chai = require('chai'); | |
const expect = chai.expect; | |
const mongoose = require('mongoose'); | |
const ObjectId = mongoose.Schema.Types.ObjectId; | |
const sinon = require('sinon'); | |
const sinonChai = require('sinon-chai'); | |
chai.use(sinonChai); | |
const ChildSchema = mongoose.Schema({ | |
}); | |
const Child = mongoose.model('Child', ChildSchema); | |
const ParentSchema = mongoose.Schema({ | |
children: [{ type: ObjectId, ref: 'Child'}], | |
parameters: mongoose.Schema.Types.Mixed, | |
}); | |
const Parent = mongoose.model('Parent', ParentSchema); | |
it('should deep match mongoose arrays', function() { | |
expect((new Parent({ children: [] }).children)).to.deep.equal([]); | |
}); | |
it('should deep match mongoose arrays on stub', function() { | |
const stub = sinon.stub(); | |
const actual = (new Parent({ children: [] })).children; | |
stub(actual); | |
expect(stub.getCall(0).args[0]).to.deep.equal([]); | |
}); | |
it('should deep match mongoose arrays on stub using called with with sinon matchers', function() { | |
const stub = sinon.stub(); | |
const actual = (new Parent({ children: [] })).children; | |
stub(actual); | |
sinon.assert.calledWith(stub, sinon.match.array.deepEquals([])); | |
}); | |
it('should deep match mongoose arrays on stub using calledWith with sinon-chai', function() { | |
const stub = sinon.stub(); | |
const actual = (new Parent({ children: [] })).children; | |
stub(actual); | |
expect(stub).to.have.been.deep.calledWith([]); | |
}); | |
it('should deep match mongoose object on stub using called with with sinon matchers', function() { | |
const stub = sinon.stub(); | |
const actual = (new Parent({ parameters: { foo: 'bar', alpha: 42 } })).parameters; | |
stub(actual); | |
sinon.assert.calledWith(stub, sinon.match({ foo: 'bar', alpha: 42 })); | |
}); | |
it('should deep match mongoose object on stub using calledWith with sinon-chai', function() { | |
const stub = sinon.stub(); | |
const actual = (new Parent({ parameters: { foo: 'bar', alpha: 42 } })).parameters; | |
stub(actual); | |
expect(stub).to.have.been.calledWith({ foo: 'bar', alpha: 42 }); | |
}); |
Author
ppg
commented
Aug 8, 2017
Update to show correct behavior only using sinon
and incorret behavior with sinon-chai
:
$ mocha mongoose-arrays.js
✓ should deep match mongoose arrays
✓ should deep match mongoose arrays on stub
✓ should deep match mongoose arrays on stub using called with with sinon matchers
1) should deep match mongoose arrays on stub using calledWith with sinon-chai
✓ should deep match mongoose object on stub using called with with sinon matchers
✓ should deep match mongoose object on stub using calledWith with sinon-chai
5 passing (41ms)
1 failing
1) should deep match mongoose arrays on stub using calledWith with sinon-chai:
AssertionError: expected stub to have been called with arguments []
[]
at Context.<anonymous> (mongoose-arrays.js:43:34)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment