Created
March 21, 2017 09:17
-
-
Save ralekna/c92be925967703b489a685c8b6c4b4f1 to your computer and use it in GitHub Desktop.
Sinon Mongoose mock example
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
import * as sinon from 'sinon'; | |
import * as Mongoose from 'mongoose'; | |
import { expect } from 'chai'; | |
// test utility | |
function areObjectIdsEqual(id1: Mongoose.Types.ObjectId | string, id2: Mongoose.Types.ObjectId | string) { | |
let normalizedId1: Mongoose.Types.ObjectId = new Mongoose.Types.ObjectId(id1.toString()); | |
let normalizedId2: Mongoose.Types.ObjectId = new Mongoose.Types.ObjectId(id2.toString()); | |
return normalizedId1.equals(normalizedId2); | |
} | |
// test utility | |
function matchGivenFields(expected) { | |
return function(actual) { | |
let result: boolean = true; | |
for (let key of Object.keys(expected)) { | |
if (expected[key] instanceof Mongoose.Types.ObjectId || actual[key] instanceof Mongoose.Types.ObjectId || key === '_id') { | |
if (!areObjectIdsEqual(expected[key], actual[key])) { | |
console.error('Mismatching ids on: ', key, ', expected: ', expected[key], 'actual: ', actual[key]); | |
result = false; | |
} | |
} else { | |
if ((actual[key] !== expected[key])) { | |
console.error('Mismatching on: ', key, ', expected: ', expected[key], 'actual: ', actual[key]); | |
result = false; | |
} | |
} | |
} | |
if (!result) { | |
console.log(actual); | |
} | |
return result; | |
}; | |
} | |
function createUser() { | |
let user = new User({name: 'John'}); | |
return user.save(); | |
} | |
function findUser(name) { | |
return User.findOne({name}); | |
} | |
describe('simple Mongoose persistence testing', () => { | |
let insertStub: sinon.SinonStub; | |
let userFindStub: sinon.SinonStub; | |
beforeEach(() => { | |
insertStub = sinon.stub(Mongoose.Collection.prototype, 'insert', function(docs, options, callback) { | |
// console.log('docs', docs); | |
callback(null, docs); | |
}); | |
userFindStub = sinon.stub(Client, 'findOne'); | |
userFindStub.withArgs(sinon.match(matchGivenFields({name: 'John'}))).returns(Promise.resolve(new User({name: 'John'}))); | |
userFindStub.withArgs({name: 'Alex'}).returns(Promise.resolve(null)); | |
userFindStub.withArgs(sinon.match.any).returns(Promise.resolve(null)); | |
}); | |
afterEach(() => { | |
insertStub.restore(); | |
userFindStub.restore(); | |
}); | |
it('should detect that mongoose model was saved', done => { | |
createUser.then(() => { | |
expect(insertStub.calledOnce).to.be.true; | |
expect(insertStub.calledWith(sinon.match(matchGivenFields({ | |
name: 'John' | |
})))).to.be.true; | |
done(); | |
}) | |
}); | |
it('should find user John', done => { | |
findUser('John').then(() => { | |
expect(userFindStub.calledOnce).to.be.true; | |
expect(userFindStub.calledWith(sinon.match(matchGivenFields({ | |
name: 'John' | |
})))).to.be.true; | |
done(); | |
}) | |
}); | |
it('should not find user John', done => { | |
findUser('John').then((user) => { | |
expect(user).to.be.null; | |
expect(userFindStub.calledOnce).to.be.true; | |
expect(userFindStub.calledWith(sinon.match(matchGivenFields({ | |
name: 'Alex' | |
})))).to.be.true; | |
done(); | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment