Skip to content

Instantly share code, notes, and snippets.

@okunishinishi
Created August 8, 2015 14: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 okunishinishi/06768ab57839e7f324e5 to your computer and use it in GitHub Desktop.
Save okunishinishi/06768ab57839e7f324e5 to your computer and use it in GitHub Desktop.
Node.jsのテストで特定の関数をモックと置き換える ref: http://qiita.com/okunishinishi@github/items/fe22839a72a5717a4982
// nodeunitのテストケース
exports.setUp = function(done){
// console.logを何もしない関数と置き換える
console.log = function noop() {
}
done();
};
// 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();
};
// 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();
};
/**
* 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;
}
};
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;
var inject = injector.inject.bind(injector);
npm install injectmock --save-dev
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