当抛出的任何错误没有冒泡时,如何在promise中进行断言?

lag*_*lex 7 mocha.js throw node.js promise

使用mocha运行会导致超时,而不是让mocha捕获错误,因此它可能会立即失败.

var when = require('when');
var should = require('should');

describe('', function() {
    it('', function(done) {
        var d = when.defer();
        d.resolve();
        d.promise.then(function() {
            true.should.be.false;
            false.should.be.true;
            throw new Error('Promise');
            done();
}); }); });
Run Code Online (Sandbox Code Playgroud)

http://runnable.com/me/U7VmuQurokZCvomD

是否有另一种方法可以在promise中进行断言,这样当它们失败时,它们会被mocha捕获,导致它立即失败?


按照chai建议,我调查了它,似乎我必须直接访问promise对象,对吧?问题是我没有直接使用承诺..如果我简化了我的坏,但这将更接近现实的例子

function core_library_function(callback){
    do_something_async(function which_returns_a(promise){
        promise.then(function(){
            callback(thing);
}); }); }

describe('', function() {
    it('', function(done) {
        core_library_function(function(thing){
            ...
            done();                         
}); }); });
Run Code Online (Sandbox Code Playgroud)

所以我真的无法直接控制这个承诺,它被抽象了很远.

Jon*_*ski 12

当使用与Mocha的promises时,你将不得不return在测试中承诺并且done因为没有使用回调而想要删除参数.

it('', function() {
    var d = when.defer();
    d.resolve();
    return d.promise.then(function() {
        throw new Error('Promise');
    });
});
Run Code Online (Sandbox Code Playgroud)

使用Promises下的文档中对此进行了描述:

或者,done()您可以返回承诺,而不是使用回调.