Last active
August 29, 2015 14:07
-
-
Save jondlm/8d9d42a426f2a605a50f to your computer and use it in GitHub Desktop.
Mocha, Chai, and Sinon Example
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
// `npm install -g mocha` | |
// `npm install chai sinon` | |
// Run with `mocha sinon_example.js` | |
var assert = require('chai').assert; | |
var sinon = require('sinon'); | |
var clock = sinon.useFakeTimers(); | |
// Sample function, this would normally be where you `require` your modules | |
var slow = function(cb) { | |
setTimeout(function() { | |
cb(null, 1); | |
}, 500); | |
}; | |
describe('main', function() { | |
it('should work with spies and fake time', function() { | |
var callback = sinon.spy(); | |
slow(callback); | |
clock.tick(550); // advance time 550 milliseconds | |
assert.isTrue(callback.called); | |
assert.equal(callback.callCount, 1); | |
assert.isTrue(callback.calledWith(null, 1)); | |
}); | |
it('should stub', function() { | |
var stub = sinon.stub().returns(25); | |
assert.equal(stub(), 25); | |
assert.isTrue(stub.calledOnce); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment