用摩卡testing承诺链

我有以下风格的function(使用Node.JS下的蓝鸟承诺):

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); }), }); 

我试图testing(使用摩卡,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(); }); }); }); 

所有这些都是这样工作的,但是对于一个人来说却感觉错综复杂。

我的主要问题是testing函数中的Promises链(和顺序)。 我已经尝试了几个东西(用一个方法伪造一个对象,这个方法不起作用,像疯了似的嘲笑它); 但是似乎还没有看到我所看到的东西,而且我似乎也没有在这方面得到摩卡或者颂文。

任何人有任何指针?

谢谢

计数

摩卡支持承诺,所以你可以做到这一点

 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"); }