当抛出的任何错误没有冒出来时,如何在Promise中做出断言? [英] How to make assertions inside a promise when any errors thrown don't bubble up?

查看:144
本文介绍了当抛出的任何错误没有冒出来时,如何在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();
}); }); });

http://runnable.com/me/U7VmuQurokZCvomD

还有没有其他方法可以在诺言中进行断言,例如当它们失败时,它们会被摩卡捕获,从而立即失败?


按照柴的建议,我调查了一下,看来我必须直接访问promise对象,对吗?问题是我没有直接使用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();                         
}); }); });

所以我真的无法直接控制诺言,它被抽象到很远的地方.

解决方案

在Mocha中使用Promise时,您必须在测试中return Promise,并且将要删除done参数,因为回调不是没有被使用.

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

文档在使用承诺下的文档中进行了描述:

或者,您可以返回一个Promise,而不是使用done()回调.

Running this with mocha results in timing out, rather than letting mocha catch the error so it could fail immediately..

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

http://runnable.com/me/U7VmuQurokZCvomD

Is there another way to make assertions inside the promise, such that when they fail they are caught by mocha causing it to fail immediately?


As per chai recommendation, I looked into it and it seems I have to have a direct access to the promise object, right? The problem is that I'm not using promise directly.. My bad if I simplified but This would be a more closer to reality example

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

So I really have no control over the promise directly, it's abstracted far far away.

解决方案

When using promises with Mocha, you'll have to return the promise in the test and will want to remove the done parameter since the callback isn't being used.

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

This is described in the docs under Working with Promises:

Alternately, instead of using the done() callback, you can return a promise.

这篇关于当抛出的任何错误没有冒出来时,如何在Promise中做出断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆