【问题标题】:asserting response body is empty断言响应正文为空
【发布时间】:2014-09-08 17:49:54
【问题描述】:

我正在尝试断言响应正文是否为空,但出现错误:

var api = supertest(TEST_URL);
...
api.get('..')
   .expect('Content-Type', /json/)
   .end(function (err, res) {
      if (err) {
        return done(err);
      }

      res.should.have.status(200);

      // Uncaught AssertionError: expected {} to have a property 'length'
      // res.body.should.empty;

      // Workaround should be used
      res.text.should.eql('{}');

怎么了?我该如何解决这个问题?

【问题讨论】:

  • 我怎样才能断言body是空的,即带有res.send()的sendet

标签: node.js supertest should.js


【解决方案1】:

should.js 中的.empty 断言检查字符串、参数、数组只是长度属性。因此,如果它抛出关于缺少长度的断言 - 你的身体看起来没有被解析为 json(你需要确保你返回正确的 Content-Type 就像 application/json 一样)。

对于对象,是的,.empty 将检查是否缺少任何可枚举的属性。

$ node
> var should = require('should')
undefined
> var res = { body: {} };
undefined
> res.body.should.be.empty
{ obj: {},
  params: { operator: 'to be empty' },
  negate: false }
> 

【讨论】:

  • res.type = "application/json", res.text = "{}", res.body = Object, res.should.be.a.json 已通过
  • @hellboy 我添加了在 REPL 中有效的示例。所以我认为你的body 有问题(我使用了最新的 should.js 4.0.4)。
猜你喜欢
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多