Created
August 8, 2015 14:56
-
-
Save okunishinishi/06768ab57839e7f324e5 to your computer and use it in GitHub Desktop.
Node.jsのテストで特定の関数をモックと置き換える ref: http://qiita.com/okunishinishi@github/items/fe22839a72a5717a4982
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
// nodeunitのテストケース | |
exports.setUp = function(done){ | |
// console.logを何もしない関数と置き換える | |
console.log = function noop() { | |
} | |
done(); | |
}; |
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
// nodeunitのテストケース | |
var log; | |
exports.setUp = function(done){ | |
// console.logを何もしない関数と置き換える | |
log = console.log; | |
console.log = function noop() { | |
} | |
done(); | |
}; | |
exports.tearDown = function(done){ | |
// console.logを戻す | |
console.log = log; | |
done(); | |
}; |
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
// Disable console.log before test started. | |
exports.setUp = function (done) { | |
function mockLog() { | |
} | |
// Inject `mockLog` as `console.log`. | |
injectmock(console, 'log', mockLog); | |
done(); | |
}; | |
// Disable console.log before test done. | |
exports.tearDown = function (done) { | |
// Restore all injected. | |
injectmock.restoreAll(); | |
console.log('Now I am back!'); | |
done(); | |
}; |
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
/** | |
* Inject mock functions to node modules. | |
* @memberof injectmock | |
* @inner | |
* @constructor Injector | |
*/ | |
function Injector() { | |
var s = this; | |
s.injections = []; | |
} | |
Injector.prototype = { | |
/** | |
* Inject a mock to a module. | |
* @param {object} module - Module to injected. | |
* @param {string} key - Injection key. | |
* @param {*} value - Value to inject. | |
* @returns {*} - Returns self. | |
*/ | |
inject: function (module, key, value) { | |
var s = this; | |
s.injections.unshift({ | |
module: module, | |
key: key, | |
delete: !module.hasOwnProperty(key), | |
origin: module[key], | |
value: value | |
}); | |
module[key] = value; | |
return s; | |
}, | |
/** | |
* Restore injected. | |
* @param {object} module - Module to injected. | |
* @param {string} key - Injection key. | |
* @returns {*} - Returns self. | |
*/ | |
restore: function (module, key) { | |
var s = this; | |
var index = s._indexOfInjection(module, key); | |
var injection = s.injections[index]; | |
if (injection) { | |
if (injection.delete) { | |
delete module[key]; | |
} else { | |
module[key] = injection.origin; | |
} | |
s.injections.splice(index, 1); | |
} else { | |
// Do nothing. | |
} | |
return s; | |
}, | |
/** | |
* Restore all injected. | |
* @returns {*} - Returns self. | |
*/ | |
restoreAll: function () { | |
var s = this; | |
while (s.injections.length) { | |
var injection = s.injections[0]; | |
s.restore(injection.module, injection.key); | |
} | |
return s; | |
}, | |
_indexOfInjection: function (module, key) { | |
var s = this; | |
for (var i = 0, len = s.injections.length; i < len; i++) { | |
var injection = s.injections[i], | |
hit = (injection.key === key) && (injection.module === module); | |
if (hit) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
}; |
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
function context() { | |
var injector = new Injector(); | |
var inject = injector.inject.bind(injector); | |
inject.restore = injector.restore.bind(injector); | |
inject.restoreAll = injector.restoreAll.bind(injector); | |
return inject; | |
} | |
var injectmock = context(); | |
module.exports = injectmock; |
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 inject = injector.inject.bind(injector); |
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
npm install injectmock --save-dev |
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 injectmock = require('injectmock'); | |
// Disable console.log before test started. | |
exports.setUp = function (done) { | |
function mockLog() { | |
} | |
// Inject `mockLog` as `console.log`. | |
injectmock(console, 'log', mockLog); | |
done(); | |
}; | |
// Disable console.log before test done. | |
exports.tearDown = function (done) { | |
// Restore all injected. | |
injectmock.restoreAll(); | |
console.log('Now I am back!'); | |
done(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment