【发布时间】:2016-08-25 12:46:31
【问题描述】:
我想测试我的服务器。 测试提供简单场景:
- 向服务器发送请求
- 等待回复
- 检查一下
我尝试使用mocha 进行测试,使用supertest 进行请求。
测试示例:
function request(url, query, cb) {
var req = supertest(app.listen())
.get(url)
.query(query)
.end(function(err, res){
if (err) {throw (err);}
cb(res);
});
}
it('Check something after response', function *(done) {
request(this.url, this.query, function(res) {/* some after response check here */});
});
现在我需要将我的回调样式代码重组为生成器样式代码。
我需要这样的东西:
it('Check something in response', function *(done) {
var res = yield request(this.url, this.query);
/* some after response check here */
});
很遗憾,我无法理解我需要在request() 中进行哪些更改
附:我不关注其他supertest 之类的具有适当风格的解决方案。我只是想了解如何用这个简单的例子来包装回调。
【问题讨论】:
标签: javascript node.js callback mocha.js generator