【问题标题】:supertest .expect(function(res) {}) what to return if test is oksupertest .expect(function(res) {}) 如果测试正常返回什么
【发布时间】:2014-05-11 16:05:44
【问题描述】:

如果有使用 mocha 和 supertest 的测试:

describe('GET /v1/news/5339a146d46d35ebe953030a --test', function(){
it('respond with json', function(done){
    request(app)
        .get('/v1/news/5339a146d46d35ebe953030a')
        .set('Accept', 'application/*')
        .set('DEV-ROLE', 'test')
        .expect('Content-Type', /json/)
        .send(json)
        .expect(200)
        .expect(function(res) {
            //THIS FUNCTION RESULT IN A TIMEOUT IF NO ERROR IS THROWN
            if(!('item' in res.body)) throw  new Error("missing item key");
        })
        .end(function(err, res){
            if (err) return done(err);
            done()

        });

    })
})

我想测试生成的主体,但我总是超时运行。 如果发生错误,我可以抛出错误,那很好。但是,如果没有发生错误,如果我返回文档中描述的任何内容,我就会遇到超时:

传递自定义断言函数。它将被赋予响应对象 去检查。如果响应正常,它应该返回虚假,最常见的是 通过不返回任何东西。如果检查失败,则抛出错误或 返回一个真实的值,比如一个会变成错误的字符串

即时文档示例:

  request(app)
    .get('/')
    .expect(hasPreviousAndNextKeys)
    .end(done);

  function hasPreviousAndNextKeys(res) {
    if (!('next' in res.body)) return "missing next key";
    if (!('prev' in res.body)) throw new Error("missing prev key");
  }

我使用的是 0.12.1 版

【问题讨论】:

    标签: node.js gruntjs mocha.js supertest


    【解决方案1】:

    等待版本 0.13.0 解决了这个问题。现在可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2023-01-24
      • 2017-11-21
      相关资源
      最近更新 更多