本文介绍了在Mocha中运行超级测试时,如何获取实际的服务器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用supertest和mocha编写了这段代码:

I have this code using supertest and mocha:

import request from 'supertest';

//....

var newGame;
describe('Creating game', function() {
  beforeEach(function(done) {
    request(app)
      .post('/api/games')
      .send({
        owner: 'Mr. X',
      })
      .expect(201)
      .expect('Content-Type', /json/)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        newGame = res.body;
        done();
      });
  });    

  describe('the created game', function() {

    it('should name the specified owner', function() {
      newGame.owner.should.equal('Mr. X');
    });

   ...
  })
});

当服务器代码引发某些异常(例如访问未定义对象的属性)时,我得到了此堆栈跟踪

When the server code throws some exception (e.g. accessing properties of an undefined object) I get this stack trace

Error: expected 201 "Created", got 500 "Internal Server Error"
  at Test._assertStatus (D:\Codes\theApp\node_modules\supertest\lib\test.js:232:12)
  at Test._assertFunction (D:\Codes\theApp\node_modules\supertest\lib\test.js:247:11)
  at Test.assert (D:\Codes\theApp\node_modules\supertest\lib\test.js:148:18)
  at Server.assert (D:\Codes\theApp\node_modules\supertest\lib\test.js:127:12)
  at emitCloseNT (net.js:1521:8)

而不是显示诸如访问未定义的属性"之类的实际错误.如何获得实际错误?

instead of the actual error that says something like "accessing properties of undefined". How can I get the actual error?

推荐答案

您可以让您的测试代码侦听该过程将引发的 uncaughtException 事件.只要express应用程序在与测试工具相同的过程中完成所有处理,则该过程中任何未处理的异常都将发送到该过程uncaughtException事件处理程序.现在,在这里可能会变得有些棘手.由于它是基于事件的,因此可以随时触发未处理的异常.因此,如果您想更明确,只想处理被测系统的异常,则需要在运行被测系统代码之前/之后添加/删除侦听器.这是更新您的示例以侦听未处理的异常的示例.

You can have your test code listen for the uncaughtException event that the process will raise. As long as the express app does all of its processing in the same process as the test harness any unhandled exception in the process will be send to the process uncaughtException event handler. Now, here's where it can get a bit tricky. Since, it's event based it can be fired at any time there is an unhandled exception. So, if you want to be more explicit and you only want to handle exceptions from the system under test, you will need to add/remove the listener before/after the system under test code is ran. Here's your example updated to listen for an unhandled exception.

import request from 'supertest';

//....

var newGame;
describe('Creating game', function() {
  beforeEach(function(done) {
    var unhandledException = undefined;
    var unhandledExceptionCallback = function(err) {
        unhandledException = err;
    }
    process.on('uncaughtException', unhandledExceptionCallback);
    request(app)
      .post('/api/games')
      .send({
        owner: 'Mr. X',
      })
      .expect(201)
      .expect('Content-Type', /json/)
      .end((err, res) => {
        process.removeListener('uncaughtException', unhandledExceptionCallback);
        if (unhandledException !== undefined){
          return done(unhandledException);
        } else if (err) {
          return done(err);
        }
        newGame = res.body;
        done();
      });
  });    

  describe('the created game', function() {

    it('should name the specified owner', function() {
      newGame.owner.should.equal('Mr. X');
    });

   ...
  })
});

这篇关于在Mocha中运行超级测试时,如何获取实际的服务器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:28