超级测试的期望值与预期值之间的差异? [英] Difference between supertest's expect and then?
问题描述
使用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 aTest
object and, when test fails, prints a large stack tracethen
returns aPromise
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屋!