简体   繁体   English

Mocha测试失败,出现AssertionError

[英]Mocha Test Fails with AssertionError

In JUnit (Java) the result of a unit test is either a succes, failure or error. 在JUnit(Java)中,单元测试的结果是成功,失败或错误。

When i try to run a test with Mocha i either get a succes or assertion error. 当我尝试使用Mocha运行测试时,我获得了成功或断言错误。

Is is normally to get an AssertionError for failure tests? 通常是为失败测试获取AssertionError吗? (shouldn't it just be called an failure and not an error?) (它不应该被称为失败而不是错误吗?)

AssertionError: -1 == 2 + expected - actual 断言错误:-1 == 2 +预期 - 实际

What about testing asynchronous code? 那么测试异步代码呢? When my tests fail i get an Uncaught eror? 当我的测试失败时,我得到一个未捕获的错误? Is that normal? 这是正常的吗?

Like this: 像这样:

Uncaught Error: expected 200 to equal 201 未捕获的错误:预计200到201等于

What you are describing is the normal behavior for Mocha. 你所描述的是摩卡的正常行为。 This code illustrates what happens if you do not try to trap exceptions in asynchronous code (even raised by assertion failures) and what you can do if you want to avoid the uncaught exception message: 此代码说明了如果您不尝试在异步代码中捕获异常(甚至由断言失败引发异常)以及如果您想要避免未捕获的异常消息,您可以执行的操作会发生什么:

var assert = require("assert");

it("fails with uncaught exception", function (done) {
    setTimeout(function () {
        assert.equal(1, 2);
        done();
    }, 1000);
});

it("fails with assertion error", function (done) {
    setTimeout(function () {
        try {
            assert.equal(1, 2);
            done();
        }
        catch (e) {
            done(e);
        }
    }, 1000);
});

The code above produces this output: 上面的代码产生了这个输出:

  1) fails
  2) fails

  0 passing (2s)
  2 failing

  1)  fails:
     Uncaught AssertionError: 1 == 2
      at null._onTimeout (/tmp/t2/test.js:5:16)
      at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

  2)  fails:
     AssertionError: 1 == 2
      at null._onTimeout (/tmp/t2/test.js:13:20)
      at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM