-
-
Save hellboy81/7587b172ea3195a3f62c to your computer and use it in GitHub Desktop.
Using rewire to deal with private methods in SUT
This file contains 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
var m_otherMod | |
/** | |
* Here is called db method of db that should be stubbed | |
* @private | |
* @param a | |
* @param b | |
* @return {*} | |
*/ | |
function privateMethodThatShouldBeTested(a, b) { | |
return m_otherMod.func(a, b) | |
} | |
/** | |
* otherMod is used indirectly | |
* @public | |
* @param a | |
* @param b | |
* @return {*} | |
*/ | |
var m_exportedFunction = function(a, b) { | |
return privateMethodThatShouldBeTested(a, b) | |
} | |
module.exports.init = function(otherMod) { | |
m_otherMod = otherMod | |
var self = { | |
exportedFunction : m_exportedFunction | |
} | |
return self | |
} |
This file contains 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
// See: http://freetymekiyan.github.io/2014/07/02/rewire/ | |
var sinon = require('sinon'), | |
chai = require('chai'), | |
sinonChai = require('sinon-chai'), | |
rewire = require('rewire'); | |
var should = chai.should() | |
chai.use(sinonChai) | |
describe('module.js', function () { | |
var mod, | |
sut, | |
sandbox, | |
otherMod, | |
privateFunc, | |
privateFuncStub | |
beforeEach(function (done) { | |
sandbox = sinon.sandbox.create() | |
// Rewire the file to be tested. | |
mod = rewire('../lib/module.js') | |
// Now all private memebers can be accessed (read and write) | |
// save private function from rewired module | |
privateFunc = mod.__get__('privateMethodThatShouldBeTested') | |
otherMod = { | |
func: sandbox.stub() | |
} | |
sut = mod.init(otherMod) | |
done() | |
}) | |
it('can stub private method in SUT', function (done) { | |
// Stub should return 4 | |
// Create stub | |
privateFuncStub = sandbox.stub() | |
// Override private methdos with stub | |
mod.__set__('privateMethodThatShouldBeTested', privateFuncStub) | |
privateFuncStub.returns(5) | |
var result = sut.exportedFunction(2,2) | |
should.exist(result) | |
result.should.be.eql(5) | |
privateFuncStub.should.have.been.called | |
done() | |
}) | |
it('can test private method', function (done) { | |
// privateFunc = mod.__get__('privateMethodThatShouldBeTested') | |
otherMod.func.returns(4) | |
var sum = privateFunc(2,2) | |
sum.should.be.eql(4) | |
otherMod.func.should.have.been.calledWith(2,2) | |
done() | |
}) | |
afterEach(function (done) { | |
// restore private method manually | |
mod.__set__('privateMethodThatShouldBeTested', privateFunc) | |
sandbox.restore() | |
done() | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment