【发布时间】:2015-02-17 00:15:52
【问题描述】:
我有以下风格的函数(在 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();
});
});
});
所有这些都可以正常工作,但感觉很复杂。
我的主要问题是在函数中测试 Promises 链(和顺序)。我已经尝试了几件事(用 then 方法伪造一个对象,这不起作用;像疯了一样嘲笑它等);但似乎有些东西我没有看到,而且我似乎没有得到这方面的 mocha- 或 sinon- 文档。
有人指点吗?
谢谢
计数
【问题讨论】:
标签: javascript node.js unit-testing mocha.js bluebird