从承诺链中的断言中捕获错误
Catching error from assert in promise chain
您好,我正在尝试捕获承诺链中的错误,例如:
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
那么,如果您输入以下内容而不是消息,会发生什么情况?
error.toString().indexOf('revert') >= 0
这成功通过了正确输出的测试。
这是来源:
MARCO BARBERO 发布于 2018 年 1 月 15 日
有时在 TypeScript 上,当您调用 indexOf() 方法时,您会得到错误“indexOf 不是一个函数”。当变量中包含的数据由没有其他字符的数字表示时,即使变量已声明为字符串,也会发生这种情况。
要解决错误,您应该使用 toString() 方法:
myStringVar.toString().indexOf('mysubstring');
您好,我正在尝试捕获承诺链中的错误,例如:
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
那么,如果您输入以下内容而不是消息,会发生什么情况?
error.toString().indexOf('revert') >= 0
这成功通过了正确输出的测试。
这是来源:
MARCO BARBERO 发布于 2018 年 1 月 15 日
有时在 TypeScript 上,当您调用 indexOf() 方法时,您会得到错误“indexOf 不是一个函数”。当变量中包含的数据由没有其他字符的数字表示时,即使变量已声明为字符串,也会发生这种情况。
要解决错误,您应该使用 toString() 方法:
myStringVar.toString().indexOf('mysubstring');