Node.js 获取错误:解析方法指定过度。指定回调*或*返回承诺;不是两者都有
当我运行我的函数时,它会给我错误:Node.js 获取错误:解析方法指定过度。指定回调*或*返回承诺;不是两者都有,node.js,mocha.js,node-modules,chai,Node.js,Mocha.js,Node Modules,Chai,当我运行我的函数时,它会给我错误:解析方法被过度指定。指定回调*或*返回承诺;并非两者都有。有人能检查我的代码并帮助解决此问题吗?我把我的完整代码放在这里 it('Get Organizations', async function (done) { let user_permission = await commonService.getuserPermission(logged_in_user_id,'organizations','findAll'); le
解析方法被过度指定。指定回调*或*返回承诺;并非两者都有。
有人能检查我的代码并帮助解决此问题吗?我把我的完整代码放在这里
it('Get Organizations', async function (done) {
let user_permission = await commonService.getuserPermission(logged_in_user_id,'organizations','findAll');
let api_status = 404;
if(user_permission) {
api_status = 200;
}
let current_token = currentResponse['token'];
old_unique_token = current_token;
chai.request(app)
.post('entities')
.set('Content-Type', 'application/json')
.set('vrc-access-token', current_token)
.send({ "key": "organizations", "operation": "findAll" })
.end(function (err, res) {
currentResponse = res.body;
expect(err).to.be.null;
expect(res).to.have.status(api_status);
done();
});
});
这是摩卡咖啡的“问题”。从代码中删除done()
。当使用像在chai.request中这样的承诺时,它是多余的
您正在返回一个承诺,因此如中所述,调用“完成”是多余的
错误消息
在旧版本中,如果使用异步方法,则必须使用回调
那样
现在你有了回报承诺的选择
From:在定义异步
单元测试函数时,不要使用done
,而只需在等待请求后,像通常那样从异步方法返回即可:
it('Get Organizations', async function () {
let user_permission = await commonService.getuserPermission(logged_in_user_id,'organizations','findAll');
let api_status = 404;
if(user_permission) {
api_status = 200;
}
let current_token = currentResponse['token'];
old_unique_token = current_token;
await chai.request(app) // note 'await' here
.post('entities')
.set('Content-Type', 'application/json')
.set('vrc-access-token', current_token)
.send({ "key": "organizations", "operation": "findAll" })
.then(function (err, res) { // not 'then', not 'end'
currentResponse = res.body;
expect(err).to.be.null;
expect(res).to.have.status(api_status);
});
});
测试运行程序将自动等待
您的函数。如果我不使用done(),那么它会给我这个错误:超时超过2000ms。对于异步测试和挂钩,确保调用“done()”;啊,似乎chai.request()可能要求您使用then()
而不是end()
来返回承诺。请看下面的“处理响应-承诺”示例:
it ('returns async', function() { return new Promise(function
(resolve) {
callAsync()
.then(function(result) {
assert.ok(result);
resolve();
}); }); })
it('Get Organizations', async function () {
let user_permission = await commonService.getuserPermission(logged_in_user_id,'organizations','findAll');
let api_status = 404;
if(user_permission) {
api_status = 200;
}
let current_token = currentResponse['token'];
old_unique_token = current_token;
await chai.request(app) // note 'await' here
.post('entities')
.set('Content-Type', 'application/json')
.set('vrc-access-token', current_token)
.send({ "key": "organizations", "operation": "findAll" })
.then(function (err, res) { // not 'then', not 'end'
currentResponse = res.body;
expect(err).to.be.null;
expect(res).to.have.status(api_status);
});
});