超级测试的期望值与预期值之间的差异? [英] Difference between supertest's expect and then?

查看:140
本文介绍了超级测试的期望值与预期值之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用supertest在JavaScript中测试异步HTTP请求时,这两个代码段之间有什么区别?其中一个是正确的,另一个是错误的吗?

When using supertest for testing async HTTP requests in JavaScript, what's the difference between these two snippets? Is one of them correct and the other one wrong?

request('http://localhost:8080/').get('/api/people') .expect(res => res.body.should.have.length(5))

request('http://localhost:8080/').get('/api/people') .expect(res => res.body.should.have.length(5))

vs.

request('http://localhost:8080/').get('/api/people') .then(res => res.body.should.have.length(5))

request('http://localhost:8080/').get('/api/people') .then(res => res.body.should.have.length(5))

我唯一注意到的区别是:

The only differences I could notice were:

  • expect返回一个Test对象,当测试失败时,将打印一个较大的堆栈跟踪
  • then返回一个Promise对象,并且在测试失败时, 打印一个小的堆栈跟踪信息
  • expect returns a Test object and, when test fails, prints a large stack trace
  • then returns a Promise object and, when test fails, prints a small stack trace

推荐答案

取决于您所使用的测试运行程序,显然会影响答案,但是类似Mocha的内容,您可以直接在自己的计算机中返回Promise测试,这将在测试通过之前等待解决.

Depending on what test runner you're using will obviously affect the answer, but something like Mocha will allow you to return the Promise directly in your test and this will wait to be resolved before the test passes.

所以,如果您有类似的事情:

So if you having something like:

describe('Your test case', function () {

  it('will wait for promise to resolve', function () {
    return request('http://localhost:8080/').get('/api/people')
      .then(res => res.body.should.have.length(5))
  })

})

在其他情况下,您实际上应该按照 https://www.npmjs.com/package/supertest 文档.

Whereas in the other instance you really should use a done callback as per https://www.npmjs.com/package/supertest docs.

这篇关于超级测试的期望值与预期值之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆