Created
January 11, 2021 09:24
-
-
Save Dunkelheit/f2cc7fa1551d85a495bb5f3b0d5a33da to your computer and use it in GitHub Desktop.
sinon's restore vs. reset
This file contains hidden or 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 sinon = require('sinon'); | |
chai.use(require('sinon-chai')); | |
const expect = chai.expect; | |
const sandbox = sinon.createSandbox(); | |
// This is the function we will be unit testing. It accepts a `database` and a `log` object | |
function initializeSomething(database, log) { | |
log.info('About to initialize something very important...'); | |
database.initialize(); | |
try { | |
database.connect(); | |
log.info('We just initialized something important.'); | |
} catch (err) { | |
log.error({ err }, 'Something went terribly wrong while initializing something very important!'); | |
} | |
} | |
// One mock object for the database | |
const database = { | |
initialize: function () { /* Do nothing */ }, | |
connect: function () { /* Do nothing */ } | |
}; | |
// One mock object for the log | |
const log = { | |
info: sandbox.spy(), | |
error: sandbox.spy() | |
}; | |
describe('Differences between sinon\'s reset and restore', () => { | |
afterEach(() => { | |
sandbox.restore(); | |
}); | |
it('Initializes and connects successfully', () => { | |
sandbox.stub(database, 'initialize').returns(); | |
sandbox.stub(database, 'connect').returns(); | |
initializeSomething(database, log); | |
console.log(`database.initialize was called ${database.initialize.callCount} time(s)`); | |
// database.initialize was called 1 time(s) | |
console.log(`database.initialize was called ${database.connect.callCount} time(s)`); | |
// database.initialize was called 1 time(s) | |
console.log(`log.info was called ${log.info.callCount} time(s)`); | |
// log.info was called 2 time(s) | |
console.log(`log.error was called ${log.error.callCount} time(s)`); | |
// log.error was called 0 time(s) | |
expect(database.initialize).to.be.calledOnce; | |
expect(database.connect).to.be.calledOnce; | |
expect(log.info).to.be.calledTwice; | |
expect(log.error).to.not.be.called; | |
}); | |
it('Initializes and handles connection errors', () => { | |
sandbox.stub(database, 'initialize').returns(); | |
sandbox.stub(database, 'connect').throws(); | |
initializeSomething(database, log); | |
console.log(`database.initialize was called ${database.initialize.callCount} time(s)`); | |
// database.initialize was called 1 time(s) | |
console.log(`database.initialize was called ${database.connect.callCount} time(s)`); | |
// database.initialize was called 1 time(s) | |
console.log(`log.info was called ${log.info.callCount} time(s)`); | |
// log.info was called 3 time(s) - wait, what!? | |
console.log(`log.error was called ${log.error.callCount} time(s)`); | |
// log.error was called 1 time(s) | |
expect(database.initialize).to.be.calledOnce; | |
expect(database.connect).to.be.calledOnce; | |
expect(log.info).to.be.calledOnce; // This assertion will fail! We expect to be called once, not thrice! | |
expect(log.error).to.be.calledOnce; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment