【问题标题】:Is it possible to add information to the error message in supertest是否可以在超测中的错误消息中添加信息
【发布时间】:2016-03-15 20:53:42
【问题描述】:

我正在使用带有 mocha 的 supertest 来测试 nodejs express 应用程序。一切都很好,希望我想要更多描述性的错误消息。这并不是说这些消息目前不好,它们不是。我只是想了解更多信息。

例如:

it('should successfully post my data and return a valid JSON', function(done) {
  .post(url)
  .set('Accept', 'application/json')
  .expect('Content-Type', /json/)
  .send(data)
  .expect(201, resultBody)
  .end(function(err, res) {
    if (err) return done(err);
    done();
  });
});

如果发生错误,导致 resultBody 与实际结果不匹配,它将打印出漂亮的+ expected - actual 消息。但我还想查看其他信息(可能是身份验证或标题)。

我目前的解决方案是执行以下操作:

  .end(function(err, res) {
    if (err) {
      console.log(res.headers);
      console.log(res.statusCode);
      console.log(res.body);
      return done(err);
    }
    done();
  });

但是console.log 消息与“it”消息内联显示,在传递/挂起/失败消息之前,而不是实际错误。

是否可以在不费力气的情况下在错误消息中添加额外的信息?

【问题讨论】:

    标签: node.js mocha.js supertest


    【解决方案1】:

    您可以在expectshould 中使用Chai 的Language Chains 来修复它,并添加Assertion Styles

    var foo = false;
    expect(foo, 'foo must be true').to.be.true;
    //AssertionError: foo must be true: expected false to be true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 2022-06-13
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多