【发布时间】:2018-03-09 12:03:23
【问题描述】:
在我的 mocha-test 套件中,我想测试一个在后台进行异步调用的功能。如何等到异步调用完成?
例如,我打了两个背靠背的电话。第一个 post 调用也在内部进行异步调用,直到异步操作完成,第二个 post 调用不会通过。
我需要以下任何一种:
1) 在两个 post 调用之间设置一个延迟,以确保第一个 post 中的异步部分是完整的。
2) 重复进行第二次 post call 直到它通过。
3) 或者如何通过 mocha-chai 测试异步调用?
下面是例子:
describe('Back to back post calls with asynchronous operation', ()=> {
it('1st and 2nd post', (done) => {
chai.request(server)
.post('/thisis/1st_post')
.send()
.end((err, res) => {
expect(res.statusCode).to.equal(200);
/* HERE I Need A Delay or a way to call
the below post call may be for 5 times */
chai.request(server)
.post('/thisis/second_post')
.send()
.end((err, res) => {
expect(res.statusCode).to.equal(200);
});
done();
});
});
});
有没有办法解决这个问题?请帮忙。
谢谢。
【问题讨论】:
标签: node.js mocha.js asynchronous-javascript