[英]Testing Promise-chains with Mocha
我有以下样式的函数(在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);
}),
});
我正在尝试测试(使用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();
});
});
});
所有这些都是这样的,但对于一个人来说却感到错综复杂。
我的主要问题是测试函数中的Promises-chain(和order)。 我尝试了几件事(用一种方法伪造一个物体,这种方法不起作用;嘲笑它就像疯了一样); 但似乎有一些我没有看到的东西,而且我似乎没有得到这方面的摩卡或颂歌文件。
有人有指点吗?
谢谢
计数
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");
});
});
});
});
这大致相当于同步代码:
try {
something.somefunc({});
assert.fail();
} catch (e) {
assert.equal(e.message, "Expecting data");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.