[英]Testing Promise-chains with Mocha
I've got the following style of function (using bluebird promises under Node.JS): 我有以下样式的函数(在Node.JS下使用bluebird promises):
module.exports = {
somefunc: Promise.method(function somefunc(v) {
if (v.data === undefined)
throw new Error("Expecting data");
v.process_data = "xxx";
return module.exports.someother1(v)
.then(module.exports.someother2)
.then(module.exports.someother3)
.then(module.exports.someother4)
.then(module.exports.someother5)
.then(module.exports.someother6);
}),
});
which I'm trying to test (using mocha, sinon, assert): 我正在尝试测试(使用mocha,sinon,断言):
// our test subject
something = require('../lib/something');
describe('lib: something', function() {
describe('somefunc', function() {
it("should return a Error when called without data", function(done) {
goterror = false;
otherexception = false;
something.somefunc({})
.catch(function(expectedexception) {
try {
assert.equal(expectedexception.message, 'Expecting data');
} catch (unexpectedexception) {
otherexception = unexpectedexception;
}
goterror = true;
})
.finally(function(){
if (otherexception)
throw otherexception;
assert(goterror);
done();
});
});
});
All of which works as such, but feels convoluted for one. 所有这些都是这样的,但对于一个人来说却感到错综复杂。
My MAIN problem is testing the Promises-chain (and order) in the function. 我的主要问题是测试函数中的Promises-chain(和order)。 I've tried several things (faking an object with a then method, which didn't work; mocking it like crazy, etc); 我尝试了几件事(用一种方法伪造一个物体,这种方法不起作用;嘲笑它就像疯了一样); but there seems to be something I'm not seeing, and I don't seem to be getting the mocha- or sinon- documentations in this regard. 但似乎有一些我没有看到的东西,而且我似乎没有得到这方面的摩卡或颂歌文件。
Anybody got any pointers? 有人有指点吗?
Thanks 谢谢
count 计数
Mocha supports promises so you could do this Mocha支持承诺,所以你可以做到这一点
describe('lib: something', function() {
describe('somefunc', function() {
it("should return a Error when called without data", function() {
return something.somefunc({})
.then(assert.fail)
.catch(function(e) {
assert.equal(e.message, "Expecting data");
});
});
});
});
This is roughly equivalent to the synchronous code: 这大致相当于同步代码:
try {
something.somefunc({});
assert.fail();
} catch (e) {
assert.equal(e.message, "Expecting data");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.