承诺链中断言的捕获错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了承诺链中断言的捕获错误相关的知识,希望对你有一定的参考价值。

嗨,我试图捕获承诺链中的错误,如:

it("Exception is thrown for Invalid Candidate",function(){
      return Election.deployed().then(function(instance){
           electionInstance = instance;
           candidateId = 99;
           return electionInstance.vote(candidateId,{from:accounts[1]});
      }).then(assert.fail).catch(function(error){
           assert(error.message.indexOf('revert') => 0,"error message must contain revert");
           return electionInstance.candidates(1);
      }).then(function(candidate1){
           var voteCount = candidate1[0];
           assert.equal(voteCount,1,"candidate1 did not recieve any votes");
           return electionInstance.candidates(2);
      }).then(function(candidate2){
           var voteCount = candidate2[0];
           assert.equal(voteCount,0,"Candidate2 didnot recieve any votes");
      });
 });

但我在error.message附近收到语法错误。无法在chaijs文档中找到任何有用的东西。我的方法有误吗?我该怎么办?错误如下

/home/chance/Ethereum_Work/VotingApplication/test/election.js:48
           assert(error.message.indexOf('revert') => 0,"error message must contain revert");
                       ^


SyntaxError: Unexpected token .
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at /home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/home/linuxbrew/.linuxbrew/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:514:10)
at /home/linuxbrew/.linuxbrew/lib/node_modules/truffle/build/webpack:/~/truffle-core/lib/test.js:125:1
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
答案

这段代码:

error.message.indexOf('revert') => 0

是一个无效的javascript表达式,因为=>用于表示胖箭头回调的声明,这不是放置回调的正确位置,因此导致错误。

你或许是这个意思吗?

error.message.indexOf('revert') === 0

或这个:

error.message.indexOf('revert') >= 0

以上是关于承诺链中断言的捕获错误的主要内容,如果未能解决你的问题,请参考以下文章

Mocha,应该 - 在测试具有承诺的异步函数时,断言错误是沉默的

拒绝后承诺链继续

承诺链中承诺之间的延迟

未捕获(承诺):错误:没有 AngularFireAuth 的提供者

未捕获(承诺)类型错误:尝试打开窗口时出现问题

未捕获(承诺)错误:请求失败,状态码为404