Skip to content

Instantly share code, notes, and snippets.

@jondlm
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jondlm/8d9d42a426f2a605a50f to your computer and use it in GitHub Desktop.
Save jondlm/8d9d42a426f2a605a50f to your computer and use it in GitHub Desktop.
Mocha, Chai, and Sinon Example
// `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