Created
December 1, 2014 15:56
-
-
Save joelgriffith/829839a2369ecc1c396d to your computer and use it in GitHub Desktop.
Chaining Sinon Stubs
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
/** | |
* 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