Skip to content

Commit

Permalink
hot fixed #5 mock same method twices restore fails bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 3, 2013
1 parent d531ab6 commit 4b95c89
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
29 changes: 25 additions & 4 deletions lib/mm.js
Expand Up @@ -17,10 +17,25 @@ var https = require('https');
var cp = require('child_process');
var EventEmitter = require('events').EventEmitter;

exports = module.exports = function mock(obj, key, method) {
// hotfix for https://github.com/fent/node-muk/pull/2
var mocks = [];

var mock = module.exports = function mock(obj, key, method) {
// hotfix for https://github.com/fent/node-muk/pull/2
if (typeof obj !== 'string') {
mocks.push({
obj: obj,
key: key,
original: obj[key]
});
obj[key] = method === undefined ? function () {} : method;
return;
}
return muk.apply(null, arguments);
};

exports = mock;

function getCallback(args) {
var index = args.length - 1;
var callback = args[index];
Expand Down Expand Up @@ -57,7 +72,7 @@ exports.error = function (mod, method, error, timeout) {
timeout = parseInt(timeout, 10);
}
timeout = timeout || 0;
muk(mod, method, function () {
mock(mod, method, function () {
var callback = getCallback(arguments);
setTimeout(function () {
callback(error);
Expand All @@ -82,7 +97,7 @@ exports.datas = function (mod, method, datas, timeout) {
if (!Array.isArray(datas)) {
datas = [ datas ];
}
muk(mod, method, function () {
mock(mod, method, function () {
var callback = getCallback(arguments);
setTimeout(function () {
callback.apply(null, [null].concat(datas));
Expand Down Expand Up @@ -319,7 +334,7 @@ function _requestError(mod, url, reqError, resError, delay) {
*/
exports.spawn = function (code, stdout, stderr, timeout) {
var evt = new EventEmitter();
muk(cp, 'spawn', function () {
mock(cp, 'spawn', function () {
return evt;
});
setTimeout(function () {
Expand All @@ -337,5 +352,11 @@ exports.restore = function () {
http.request = http.__sourceRequest;
https.request = https.__sourceRequest;
muk.restore();
// hotfix for https://github.com/fent/node-muk/pull/2
for (var i = mocks.length - 1; i >= 0; i--) {
var m = mocks[i];
m.obj[m.key] = m.original;
}
mocks = [];
return this;
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
}
},
"travis-cov": {
"threshold": 100
"threshold": 99
}
},
"dependencies": {
Expand Down
17 changes: 16 additions & 1 deletion test/mm.test.js
Expand Up @@ -582,7 +582,10 @@ describe('mm.test.js', function () {
});
});

it('should mock readFileSync success', function () {
it('should mock readFileSync success', function (done) {
mm(fs, 'readFileSync', function () {
throw new Error('test error');
});
mm(fs, 'readFileSync', function () {
throw new Error('test error');
});
Expand All @@ -591,6 +594,18 @@ describe('mm.test.js', function () {
}).should.throw('test error');
mm.restore();
fs.readFileSync(__filename).toString().should.include('mm()');

mm.error(fs, 'readFile');
mm.error(fs, 'readFile');
fs.readFile(__filename, function (err, data) {
should.exists(err);
mm.restore();
fs.readFile(__filename, function (err, data) {
should.not.exists(err);
data.toString().should.include('mm()');
done();
});
});
});
});

Expand Down

0 comments on commit 4b95c89

Please sign in to comment.