eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.9k stars 1.82k forks source link

controller单元测试在配置onerror后超时 #4232

Closed shinji3887 closed 4 years ago

shinji3887 commented 4 years ago

对默认的异常返回进行了处理,然后单元测试就不好用了 config.default.js配置:

all(err, ctx) {
      // 在此处定义针对所有响应类型的错误处理方法
      // 注意,定义了 config.all 之后,其他错误处理方法不会再生效
      const errmsg = {};
      errmsg.name = err.name;
      errmsg.message = err.message;
      errmsg.code = err.code;
      errmsg.errors = err.errors;

      ctx.body = errmsg;
      if(errmsg.name==='UnprocessableEntityError' || errmsg.name === 'Error'){
        ctx.status = 422;
      }
      else{
        ctx.status = 500;
      }

    },

controller代码:

  async add(ctx) {

    ctx.validate(ctx.rule.createRoomRequest);
    ctx.body = await ctx.service.room.saveRoom(ctx.request.body);

  }

单元测试代码:

  it('新增会议室,房间号未输入,应返回422', () => {
    const req = {
      dept: 'XK',
      equip: '投影仪,视频会议',
      img: 'string',
    };
    return app.httpRequest()
      .post('/rooms')
      .send(req)
      .expect(422);
  });

错误日志:

2020-03-25 11:20:49,369 WARN 31592 [-/127.0.0.1/-/39ms POST /rooms] nodejs.UnprocessableEntityError: Validation Failed
    at Object.throw (F:\eggjs\mwa-meet-svc\node_modules\koa\lib\context.js:97:11)
    at Object.validate (F:\eggjs\mwa-meet-svc\node_modules\egg-validate\app\extend\context.js:14:17)
    at RoomController.add (F:\eggjs\mwa-meet-svc\app\controller\room.js:35:9)
    at Object.callFn (F:\eggjs\mwa-meet-svc\node_modules\egg-core\lib\utils\index.js:44:21)
    at Object.classControllerMiddleware (F:\eggjs\mwa-meet-svc\node_modules\egg-core\lib\loader\mixin\controller.js:87:20)
    at Object.callFn (F:\eggjs\mwa-meet-svc\node_modules\@eggjs\router\lib\utils.js:12:21)
    at wrappedController (F:\eggjs\mwa-meet-svc\node_modules\@eggjs\router\lib\egg_router.js:322:18)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa-compose\index.js:44:32)
    at next (F:\eggjs\mwa-meet-svc\node_modules\koa-compose\index.js:45:18)
    at F:\eggjs\mwa-meet-svc\node_modules\@eggjs\router\lib\router.js:190:18
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa-compose\index.js:44:32)
    at F:\eggjs\mwa-meet-svc\node_modules\koa-compose\index.js:36:12
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\@eggjs\router\lib\router.js:195:33)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa\node_modules\koa-compose\index.js:42:32)
    at F:\eggjs\mwa-meet-svc\node_modules\koa-static-cache\index.js:51:69
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa\node_modules\koa-compose\index.js:42:32)
    at i18n (F:\eggjs\mwa-meet-svc\node_modules\egg-i18n\app\middleware\i18n.js:13:12)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa\node_modules\koa-compose\index.js:42:32)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\egg-security\node_modules\koa-compose\index.js:42:32)
    at dta (F:\eggjs\mwa-meet-svc\node_modules\egg-security\lib\middlewares\dta.js:12:12)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\egg-security\node_modules\koa-compose\index.js:42:32)
    at xframe (F:\eggjs\mwa-meet-svc\node_modules\egg-security\lib\middlewares\xframe.js:7:11)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\egg-security\node_modules\koa-compose\index.js:42:32)
    at xssProtection (F:\eggjs\mwa-meet-svc\node_modules\egg-security\lib\middlewares\xssProtection.js:7:11)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\egg-security\node_modules\koa-compose\index.js:42:32)
    at nosniff (F:\eggjs\mwa-meet-svc\node_modules\egg-security\lib\middlewares\nosniff.js:8:11)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\egg-security\node_modules\koa-compose\index.js:42:32)
    at noopen (F:\eggjs\mwa-meet-svc\node_modules\egg-security\lib\middlewares\noopen.js:8:11)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\egg-security\node_modules\koa-compose\index.js:42:32)
    at notAllow (F:\eggjs\mwa-meet-svc\node_modules\egg-security\lib\middlewares\methodnoallow.js:21:12)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\egg-security\node_modules\koa-compose\index.js:42:32)
    at F:\eggjs\mwa-meet-svc\node_modules\egg-security\node_modules\koa-compose\index.js:34:12
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa\node_modules\koa-compose\index.js:42:32)
    at clusterAppMock (F:\eggjs\mwa-meet-svc\node_modules\egg-mock\app\middleware\cluster_app_mock.js:9:58)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa\node_modules\koa-compose\index.js:42:32)
    at session (F:\eggjs\mwa-meet-svc\node_modules\koa-session\index.js:41:13)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa\node_modules\koa-compose\index.js:42:32)
    at overrideMethod (F:\eggjs\mwa-meet-svc\node_modules\koa-override\index.js:38:12)
    at dispatch (F:\eggjs\mwa-meet-svc\node_modules\koa\node_modules\koa-compose\index.js:42:32)
    at bodyParser (F:\eggjs\mwa-meet-svc\node_modules\koa-bodyparser\index.js:86:11)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
message: "Validation Failed"
code: "invalid_param"
errors: [{"message":"required","field":"roomNo","code":"missing_field"}]
pid: 31592
hostname: DESKTOP-RNQGUFA

2020-03-25 11:20:49,375 ERROR 31592 nodejs.TypeError: First argument must be a string or Buffer
    at ServerResponse.end (_http_outgoing.js:739:13)
    at Object.app.context.onerror (F:\eggjs\mwa-meet-svc\node_modules\koa-onerror\index.js:92:14)
    at onerror (F:\eggjs\mwa-meet-svc\node_modules\koa\lib\application.js:163:32)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

pid: 31592
hostname: DESKTOP-RNQGUFA

2020-03-25 11:20:49,377 ERROR 31592 nodejs.TypeError: First argument must be a string or Buffer
    at ServerResponse.end (_http_outgoing.js:739:13)
    at Object.app.context.onerror (F:\eggjs\mwa-meet-svc\node_modules\koa-onerror\index.js:92:14)
    at onerror (F:\eggjs\mwa-meet-svc\node_modules\koa\lib\application.js:163:32)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

pid: 31592
hostname: DESKTOP-RNQGUFA

    1) 新增会议室,房间号未输入,应返回422

  0 passing (1m)
  1 failing

  1) room controller单元测试
       新增会议室,房间号未输入,应返回422:
     Error: Timeout of 60000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (F:\eggjs\mwa-meet-svc\test\app\controller\room.test.js)
      [use `--full-trace` to display the full stack trace]

{ Error: F:\eggjs\mwa-meet-svc\node_modules\egg-bin\node_modules\mocha\bin\_mocha --declarations,--timeout=60000,--exit,--require=F:\eggjs\mwa-meet-svc\node_modules\egg-bin\lib\mocha-clean.js,--require=F:\eggjs\mwa-meet-svc\node_modules\co-mocha\lib\co-mocha.js,--require=F:\eggjs\mwa-meet-svc\node_modules\intelli-espower-loader\intelli-espower-loader.js,test/app/controller/room.test.js exit with code 1
    at ChildProcess.proc.once.code (F:\eggjs\mwa-meet-svc\node_modules\common-bin\lib\helper.js:56:21)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12) code: 1 }

如果去除config里面的all配置,是可以正常测试通过的。

egg-bot commented 4 years ago

Hello @shinji3887. Please provide a reproducible example following the instruction.

Issues labeled by Need Reproduce will be closed if no activities in 7 days.


@shinji3887,请根据这个说明提供最小可复现代码。

如果在 7 天内没有进展会被自动关闭。

shinji3887 commented 4 years ago

已解决,需要添加accept方法,指定返回数据为json即可

accepts() {
      // 在此处定义针对所有响应类型的错误处理方法
      // 注意,定义了 config.all 之后,其他错误处理方法不会再生效
      //ctx.body = 'error';
      //ctx.status = 500;
      return 'json';
},
atian25 commented 4 years ago

app.httpRequest().set('Accept', 'application/json')

qingfengmy commented 4 years ago

解决了我的问题,感谢!