我有如下形式的函数(使用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和assert):
// 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();
});
});
});
所有这些都可以正常工作,但对于一个人来说感觉有些复杂。
我的主要问题是测试函数中 Promise 链的顺序。我尝试了几种方法(例如伪造一个具有 then 方法的对象,但无效;疯狂地模拟等),但似乎有些问题我没看到,在这方面我好像也没有获得关于 mocha 或 sinon 的文档信息。有人能提供一些指引吗?
谢谢。
计数