Skip to content

Instantly share code, notes, and snippets.

@jondlm

jondlm/sinon_example.js

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.