[英]Uncaught (in promise) error when testing promise with Mocha
我很难测试基于promise的功能。 我正在使用Mocha作为测试框架,并使用chai库作为断言框架。 我是摩卡咖啡和蔡的新手。 我遇到的问题很少,我也不知道我的代码有问题,也许我的测试方法完全错误,也许有人帮助了他们。
我遇到了Uncaught(承诺)错误,这是预期的,但是实际上我不知道我的方式是否是测试这些功能的正确方式。
这是我的returnResult函数,它解析一个值->一个字符串
var returnResult = function (stateParamName, stateParamValue) {
return new Promise((resolve, reject) => {
peer3.get({ path: { equals: 'todo/#0' } }).then(function (results) {
console.log(stateParamName + ': ' + results[0].value[stateParamName])
resolve(results[0].value[stateParamName]);
});
});
}
这是我的摩卡咖啡测试
describe("Call Tests Suite", function () {
describe("Basic Call Test", function () {
it("Call status for both side should be IN CALL", function () {
peer3.call('login/add', ['testaccount1'])
.then(() => peer3.call('todo/makeCall1', ['testaccount2@testdomain.com']))
.then(() => checkResult('state_term', 'RINGING'))
.then((interval) => { clearInterval(interval); peer3.call('todo/answerCall2', ['empty']) })
.then(() => checkResult('state_term', 'IN_CALL'))
.then((interval) => clearInterval(interval))
//.then(console.log('test sonucu: ' + returnResult('state_term', 'IN_CALL')))
.then(returnResult('state_term', 'IN_CALL'))
.then((result) => expect(result).to.equal('IN_CALL'))
});
});
如您所见,我仅将assert用于最后的结果。 也许我应该将整个测试作为promise函数进行测试。 有人可以帮我吗?
我不知道您的错误来自哪里。 但是,您的代码中有很多可以改进的地方,并且可能会导致您进行更好的调试,因此您可以找到错误:
1-您应在.catch
链的末尾添加.catch
处理程序。 “ 未捕获的错误”是指:您then
一个处理程序中有一个错误,但是没有被catch
。 您应该添加catch
在列车结束通话:
describe("Call Tests Suite", function () {
describe("Basic Call Test", function () {
it("Call status for both side should be IN CALL", function () {
peer3.call('login/add', ['testaccount1'])
.then(() => peer3.call('todo/makeCall1', ['testaccount2@testdomain.com']))
.then(() => checkResult('state_term', 'RINGING'))
.then((interval) => { clearInterval(interval); peer3.call('todo/answerCall2', ['empty']) })
.then(() => checkResult('state_term', 'IN_CALL'))
.then((interval) => clearInterval(interval))
//.then(console.log('test sonucu: ' + returnResult('state_term', 'IN_CALL')))
.then(returnResult('state_term', 'IN_CALL'))
.then((result) => expect(result).to.equal('IN_CALL'))
.catch(err => {
console.log(err);//This will allow you better debugging.
})
});
});
好的,现在,我们必须记住您的代码是异步的。 但是, 默认情况下 ,mocha it
函数是同步的:它们不会等待异步代码执行。
为了告诉mocha您的测试是异步的,您必须将参数传递给测试。 此参数是一个函数,通常称为done
,在测试完成时必须显式调用。 否则,您的测试将在到达代码的异步部分之前完成,通常会给您带来误报。
describe("Call Tests Suite", function () {
describe("Basic Call Test", function () {
it("Call status for both side should be IN CALL", function (done) {
peer3.call('login/add', ['testaccount1'])
.then(() => peer3.call('todo/makeCall1', ['testaccount2@testdomain.com']))
.then(() => checkResult('state_term', 'RINGING'))
.then((interval) => { clearInterval(interval); peer3.call('todo/answerCall2', ['empty']) })
.then(() => checkResult('state_term', 'IN_CALL'))
.then((interval) => clearInterval(interval))
//.then(console.log('test sonucu: ' + returnResult('state_term', 'IN_CALL')))
.then(returnResult('state_term', 'IN_CALL'))
.then((result) => expect(result).to.equal('IN_CALL'))
.then( () => {
done(); //dont use .then(done) or things may break due to extra
parameter
})
.catch( err => {
console.log(err);
done(err); //passing a parameter to done makes the test fail.
})
});
});
不过,您的代码仍有问题,我们必须解决。 then
方法将函数作为参数。 但是,在此行中:.then(returnResult('state_term','IN_CALL'))
您正在传递给它一个对returnResult('state_term', 'IN_CALL')
的调用,该返回不是函数,而是一个promise。 您应该改为传递一个函数-然后返回promise-:
.then(() => returnResult('state_term', 'IN_CALL')) //now the parameter to
//then is a function that return a Promise, not a Promise itself.
另外,正如您在注释中所告诉的那样,您将显式返回一个新的Promise,该Promise将一个Promise包装在return result
函数中:根本不需要,并且该函数可以这样编写:
var returnResult = function (stateParamName, stateParamValue) {
return peer3.get({ path: { equals: 'todo/#0' } }).then(function (results) {
console.log(stateParamName + ': ' + results[0].value[stateParamName]);
return(results[0].value([stateParamName]));
});
}
但是,您的代码中有很多事情看起来真的很奇怪(我根本不确定为什么存在setinterval
调用),因此我非常有信心测试不会按您预期的那样进行。 您应该首先熟悉Promises和异步Mocha测试,而不要尝试测试很长的异步操作序列。 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.