【问题标题】:AssertionError crashes MochaAssertionError 使 Mocha 崩溃
【发布时间】:2013-09-28 11:28:41
【问题描述】:

我正在使用 Mocha 运行单元测试,而不是在报告器中显示所有抛出的 AssertionErrors,Mocha 在第一个错误时崩溃。有什么建议吗?

我在崩溃时遇到的错误是:

/Users/Robert/Code/JRJ/Server/node_modules/chai/lib/chai/assertion.js:106
      throw new AssertionError(msg, {
            ^
AssertionError: expected 200 to equal 202
npm ERR! weird error 8
npm ERR! not ok code 0

无论我使用 Chai 还是内置的 assert 库,都是一样的。我用这个命令运行 Mocha(我用npm test 运行它):

mocha --reporter 'spec' --recursive

我使用的库版本如下:

  • 节点:0.10.18
  • 摩卡:1.12.0
  • 柴:1.8.0
  • hapi: 1.10.0

测试代码:

    var hapi = require('hapi'),
        expect = require('chai').expect,
        assert = require('assert');

    describe("Customer API", function(){
      var server = require('../../../../src/apis/customer');

      //works as expected 
      describe('simpleExample', function(){
        it("should cause a test failure", function(done){
            expect(200).to.equal(202);
            done();
        });
      });

      //crashes Mocha
      describe('Authentication', function(){
        it('Should get user token', function(done){
          server.inject("/auth?username=test@test.com&password=testa", function(res){
            expect(res.statusCode).to.equal(202); //returns 200, crashes Mocha (the expected 202 is intentional to cause an assertion error)
            //assert.ok(res.statusCode === 202);
            expect(res.payload).to.be.a('string');
            expect(res.payload).to.have.length(16);
            done();
          });
        });
      });
    });

【问题讨论】:

    标签: node.js testing mocha.js


    【解决方案1】:

    这是因为这是 Mocha 的工作方式。异步调用中的异常需要被捕获并传递给 done 回调,这甚至包括 AssertionErrors。 Mocha 文档中有一个错误,我已经打开了一个 GitHub 问题来解决这个问题 (https://github.com/visionmedia/mocha/issues/982)。

    【讨论】:

      猜你喜欢
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      相关资源
      最近更新 更多