Skip to content

Instantly share code, notes, and snippets.

@joelgriffith
Created December 1, 2014 15:56
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 joelgriffith/829839a2369ecc1c396d to your computer and use it in GitHub Desktop.
Save joelgriffith/829839a2369ecc1c396d to your computer and use it in GitHub Desktop.
Chaining Sinon Stubs
/**
* test.mpsec.js
*/
var sinon = require('sinon');
var expect = require('chai').expect;
var rewire = require('rewire');
var main = rewire('main');
var CoolModuleMock = (function() {
function CoolModuleMock() {}
CoolModuleMock.prototype.start = sinon.stub().returns(CoolModuleMock.prototype);
CoolModuleMock.prototype.pause = sinon.stub().returns(CoolModuleMock.prototype);
CoolModuleMock.prototype.end = sinon.stub().returns(CoolModuleMock.prototype);
return CoolModuleMock;
})();
// Overwrite the `CoolModule` reference to refer to our mock
main.set__('CoolModule', CoolModuleMock);
describe('Cool Module', function() {
it('should call the `start`, `pause`, and `end` methods', function() {
main();
expect(CoolModuleMock.start.called).to.be.true();
expect(CoolModuleMock.pause.called).to.be.true();
expect(CoolModuleMock.end.called).to.be.true();
});
});
/**
* main.js
*/
var CoolModule = require('cool-module'); // rewire will overwrite this
var cool = new CoolModule();
module.exports = function() {
cool.start();
cool.pause();
cool.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment