Skip to content

Commit

Permalink
fix test in node 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Oct 30, 2014
1 parent 8928d43 commit 63785a3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 71 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
},
"dependencies": {
"co-sleep": "~0.0.1",
"enable": "~1.0.1",
"enable": "~1.0.2",
"is-type-of": "~0.3.1",
"muk": "~0.3.1",
"semver": "~4.1.0",
"thunkify-wrap": "~1.0.2"
"thunkify-wrap": "~1.0.3"
},
"devDependencies": {
"autod": "*",
Expand Down
2 changes: 1 addition & 1 deletion test/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* MIT Licensed
*/

"use strict";
'use strict';

/**
* Module dependencies.
Expand Down
70 changes: 2 additions & 68 deletions test/mm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* MIT Licensed
*/

"use strict";
'use strict';

/**
* Module dependencies.
Expand All @@ -21,7 +21,6 @@ var pedding = require('pedding');
var ChunkStream = require('chunkstream');
var mm = require('../');
var foo = require('./foo');
var thunkify = require('thunkify-wrap');

describe('mm.test.js', function () {

Expand Down Expand Up @@ -743,75 +742,10 @@ describe('mm.test.js', function () {
});
});
});

describe('thunk', function () {
var foo = {
getMultiValues: thunkify(function (arg, callback) {
setImmediate(function () {
callback(null, 1, 2, 3);
});
}),
getValue: thunkify(function (arg, callback) {
setImmediate(function () {
callback(null, 1);
});
}),
};

afterEach(mm.restore);

describe('datas(), data()', function () {
it('should mock generator function', function* () {
mm.datas(foo, 'getMultiValues', [ 'b1', 'b2', 'b3' ]);
var datas = yield foo.getMultiValues();
datas.should.eql([ 'b1', 'b2', 'b3' ]);

mm.datas(foo, 'getMultiValues', 1);
var datas = yield foo.getMultiValues('key');
datas.should.equal(1);

mm.data(foo, 'getValue', 2);
var data = yield foo.getValue('key');
data.should.equal(2);

mm.restore();
var data = yield foo.getValue('key');
data.should.equal(1);
});
});

describe('error()', function () {
it('should mock error', function* () {
mm.error(foo, 'getValue');
try {
yield foo.getValue();
throw new Error('should not run this');
} catch (err) {
err.message.should.equal('mm mock error');
}

mm.error(foo, 'getValue', 'foo error', 200);
try {
yield foo.getValue();
throw new Error('should not run this');
} catch (err) {
err.message.should.equal('foo error');
}

mm.error(foo, 'getValue', new Error('new foo error'));
try {
yield foo.getValue();
throw new Error('should not run this');
} catch (err) {
err.message.should.equal('new foo error');
}
});
});
});

});

var enable = require('enable');
if (enable.generator) {
require('./es6');
require('./thunk');
}
83 changes: 83 additions & 0 deletions test/thunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**!
* mm - test/thunk.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com>
*/

'use strict';

/**
* Module dependencies.
*/

var thunkify = require('thunkify-wrap');
var mm = require('..');

describe('thunk.js', function () {
var foo = {
getMultiValues: thunkify(function (arg, callback) {
setImmediate(function () {
callback(null, 1, 2, 3);
});
}),
getValue: thunkify(function (arg, callback) {
setImmediate(function () {
callback(null, 1);
});
}),
};

afterEach(mm.restore);

describe('datas(), data()', function () {
it('should mock generator function', function* () {
mm.datas(foo, 'getMultiValues', [ 'b1', 'b2', 'b3' ]);
var datas = yield foo.getMultiValues();
datas.should.eql([ 'b1', 'b2', 'b3' ]);

mm.datas(foo, 'getMultiValues', 1);
var datas = yield foo.getMultiValues('key');
datas.should.equal(1);

mm.data(foo, 'getValue', 2);
var data = yield foo.getValue('key');
data.should.equal(2);

mm.restore();
var data = yield foo.getValue('key');
data.should.equal(1);
});
});

describe('error()', function () {
it('should mock error', function* () {
mm.error(foo, 'getValue');
try {
yield foo.getValue();
throw new Error('should not run this');
} catch (err) {
err.message.should.equal('mm mock error');
}

mm.error(foo, 'getValue', 'foo error', 200);
try {
yield foo.getValue();
throw new Error('should not run this');
} catch (err) {
err.message.should.equal('foo error');
}

mm.error(foo, 'getValue', new Error('new foo error'));
try {
yield foo.getValue();
throw new Error('should not run this');
} catch (err) {
err.message.should.equal('new foo error');
}
});
});
});

0 comments on commit 63785a3

Please sign in to comment.