NodeJS - 拒绝Promise时JSON中缺少键

时间:2018-05-11 22:05:45

标签: json node.js promise mocha assert

我正在尝试为我的功能创建一个单元测试,我面临的问题是从拒绝承诺的JSON主体读取数据。

在我所拥有的功能中:

`return new Promise((resolve, reject) => {
               .....
                return reject({
                    'code': ID_NOT_VALID,
                    'message': 'Required header has invalid value'
                })`

在我的测试中我正在做下一步:

 const errJson = {
        code: ID_NOT_VALID,
        message: 'Required header has invalid value'
    };
......

it('should not be empty', async () => {
        return assert.isRejected(validate.validateCorrelationId(), errJson, errJson);
    });

结果,我发现由于某种原因,响应只包含“消息”参数,而不是“代码”而不是JSON格式。

AssertionError: expected promise to be rejected with an error including { Object (code, message) } but got 'Required header has invalid value'

由于某种原因看起来任何其他键被忽略但是“消息”。我究竟做错了什么?如何作为Promise拒绝的一部分返回并验证JSON?

完整代码如下:

  1. 功能:
  2. exports.validate = (strId) => {
      return new Promise((resolve, reject) => {
        if (!strId) {
            return reject({
                'code': constants.ID_NOT_VALID,
                'message': 'Required header is not present in request'
            });...........
    
    1. 测试:
    2.   

      const chai = require('chai');

          const chaiAsPromised = require("chai-as-promised");
           const expect = chai.expect;
           const assert = chai.assert;
           chai.use(chaiAsPromised);
      
      describe('validate', () => {
      const errJson = {
          'code': constants.ID_NOT_VALID,
          'message':'Required header has invalid value'
      };
      
      it('should not be empty', async () => {
          return assert.isRejected(validate.validateCorrelationId(), errJson, errJson);
      });......
      

      //下一个测试工作:

      it('should be valid ID', async () => {
              const result = await validate.validate('352d-11e8');
              expect(result).to.equal('352d-11e8');
          });
      

0 个答案:

没有答案