Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用摩卡测试承诺链_Javascript_Node.js_Unit Testing_Mocha.js_Bluebird - Fatal编程技术网

Javascript 用摩卡测试承诺链

Javascript 用摩卡测试承诺链,javascript,node.js,unit-testing,mocha.js,bluebird,Javascript,Node.js,Unit Testing,Mocha.js,Bluebird,我有以下风格的函数(使用Node.JS下的bluebird承诺): 我正在尝试测试(使用mocha、sinon和assert): 所有这些都是如此,但其中一个让人感到困惑 我的主要问题是测试函数中的承诺链(和顺序)。我试过几种方法(用then方法伪造一个对象,但不起作用;疯狂地模仿它,等等);但似乎有些东西我没有看到,我似乎没有得到摩卡咖啡或锡诺咖啡在这方面的记录 有人有什么建议吗 谢谢 count摩卡支持承诺,因此您可以做到这一点 describe('lib: something', func

我有以下风格的函数(使用Node.JS下的bluebird承诺):

我正在尝试测试(使用mocha、sinon和assert):

所有这些都是如此,但其中一个让人感到困惑

我的主要问题是测试函数中的承诺链(和顺序)。我试过几种方法(用then方法伪造一个对象,但不起作用;疯狂地模仿它,等等);但似乎有些东西我没有看到,我似乎没有得到摩卡咖啡或锡诺咖啡在这方面的记录

有人有什么建议吗

谢谢


count

摩卡支持承诺,因此您可以做到这一点

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

在这种情况下,我如何模仿其他人1-6,这样实际的实现就不会被调用?嗯。如果捕获中的断言失败,我将得到一个“可能未处理的断言错误”,我如何才能正确地让它冒泡出来?@berlincount您使用的是支持承诺的mocha版本吗?如果是,您确定您是否像回答中那样返回承诺链。使用Promise.onPossiblyUnhandledRejection(函数(错误){throw error;});根据文档帮助。
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");
}