使摩卡测试显示实际错误 [英] Make mocha tests show the actual error

查看:130
本文介绍了使摩卡测试显示实际错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Mocha感到沮丧的一件事是,当测试失败时,它们不会给出失败行的实际错误消息,相反,它们只会以 Error结尾:超过2000ms的超时时间.确保在此测试中调用了done()回调.

One of the things that I find frustrating about Mocha is that when tests fail, they don't give the actual error message of the failing line, Instead, they just end with Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

以该测试为例:

describe("myTest", function() {
    it("should return valid JSON.", function(done) {
        api.myCall("valid value").then(function(result) {
            console.log(result);
            var resultObj = JSON.parse(result);
            assert.isFalse(resultObj.hasOwnProperty("error"), "result has an error");
            done();
        });
    });
});

输出为:

  myTest
{"error":null,"status":403}
    1) should return valid JSON.


  0 passing (2s)
  1 failing

  1) myTest should return valid JSON.:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

assert.isFalse失败,但是未显示应显示的消息(结果有错误").实际上,处理似乎就在那里停止,因为从未调用过done().删掉那一行,因为调用了done(),所以测试通过了.

The assert.isFalse is failing, but the message that should be displayed ("result has an error") isn't displayed. In fact, processing seems to stop right there because done() is never called. Take that line out and the test passes because done() is called.

那么,我想念什么?为什么Mocha测试会以这种方式表现?我正在使用的实际测试库是:

So, what am I missing? Why do Mocha tests behave this way? The actual test library I'm using is:

var assert = require("chai").assert; 

有人知道我在做错什么吗,还是为什么这样做呢?

Does anyone know what I'm doing wrong or why this behaves this way?

推荐答案

您的API似乎正在使用promises.在尝试任何其他操作之前,我建议您检查一下API文档中有关承诺的内容以及如何处理未处理的异常的信息,因为这可能就是这里发生的情况.一些promise实现要求您在调用链的末尾调用.done(),以确保将处理未捕获的异常.有些要求正确配置某些全局诺言设置.蓝鸟文档对问题进行了很好的讨论.

It looks like your API is using promises. Before trying anything else, I would suggest checking what the documentation of the API says about promises and how to deal with unhandled exceptions because this may be what is happening here. Some promise implementations require that you call .done() at the end of your call chain to ensure that uncaught exceptions are going to be processed. Some require that some global promise setting be properly configured. The Bluebird documentation gives a good discussion of the issues.

Mocha能够处理常规代码中未捕获的异常:

Mocha is capable of handling uncaught exceptions in run-of-the-mill code:

var chai = require("chai");
var assert = chai.assert;
chai.config.includeStack = true;

describe("foo", function() {
    it("let the exception be caught by Mocha", function(done) {
        setTimeout(function () {
            assert.isFalse(true, "foo");
            done();
        }, 1000);
    });
});

这将导致输出:

  foo
    1) let the exception be caught by Mocha


  0 passing (1s)
  1 failing

  1) foo let the exception be caught by Mocha:
     Uncaught AssertionError: foo: expected true to be false
      at Assertion.<anonymous> (/tmp/t7/node_modules/chai/lib/chai/core/assertions.js:286:10)
      at Assertion.Object.defineProperty.get (/tmp/t7/node_modules/chai/lib/chai/utils/addProperty.js:35:29)
      at Function.assert.isFalse (/tmp/t7/node_modules/chai/lib/chai/interface/assert.js:297:31)
      at null._onTimeout (/tmp/t7/test.js:8:20)
      at Timer.listOnTimeout (timers.js:119:15)

这篇关于使摩卡测试显示实际错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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