本文介绍了如何在摩卡车上测试未捕获的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试以下函数的执行情况:

I would like to test that the following function performs as expected:

function throwNextTick(error) {
    process.nextTick(function () {
        throw error;
    });
}

这是我的尝试:

describe("throwNextTick", function () {
    it("works as expected", function (next) {
        var error = new Error("boo!");
        var recordedError = null;
        process.once("uncaughtException", function (error) {
            recordedError = error;
        });

        throwNextTick(error);

        process.nextTick(function () {
            recordedError.should.be(error);
            next();
        });
    });
});

但是,摩卡似乎想要保留自己的错误,并且在获得它们时失败我的测试:

But mocha seems to want to keep any errors to itself, and fail my test when it gets them:

C:\Users\ddenicola\Programming (Synced)\pubit>mocha test/basicTest.js

  throwNextTick
    0) works as expected

  ? 1 of 1 tests failed:

  1) throwNextTick works as expected:
     Error: boo!
      at Test.fn (C:\Users\ddenicola\Programming (Synced)\pubit\test\basicTest.js:11:21)
      at Test.run (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:144:15)
      at Runner.runTest (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:271:10)
      at C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:315:12
      at next (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:199:14)
      at C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:208:7
      at next (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:157:23)
      at Array.0 (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:176:5)
      at EventEmitter._tickCallback (node.js:192:40)

任何想法?

推荐答案

更新:在以下评论中由Casey-foster提供:

Update: Courtesy of casey-foster in a comment below:






旧答案:


Old answer:

秘密在于process.listeners 'uncaughtException'):

The secret lies in process.listeners('uncaughtException'):

只需删除摩卡监听器,添加自己的,然后重新安装摩卡监听器。

Simply remove the mocha listener, add your own, then reattach the mocha listener.

请参见下文:

var assert = require('assert')

function throwNextTick(error) {
    process.nextTick(function () {
        throw error
    })
}


describe("throwNextTick", function () {
    it("works as expected", function (next) {
        var error = new Error("boo!")
        var recordedError = null
        var originalException = process.listeners('uncaughtException').pop()
        //Needed in node 0.10.5+
        process.removeListener('uncaughtException', originalException);
        process.once("uncaughtException", function (error) {
            recordedError = error
        })
        throwNextTick(error);
        process.nextTick(function () {
            process.listeners('uncaughtException').push(originalException)
            assert.equal(recordedError, error)
            next()
        })
    })
})

这篇关于如何在摩卡车上测试未捕获的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 09:49