I have contract with methods that check input parameters using require(bool)
so if at least on of conditions does not matched it throws an error. During test procedure (using Truffle and Chaijs) it looks like:
Error: VM Exception while processing transaction: invalid opcode
at Object.InvalidResponse (/usr/lib/node_modules/truffle/build/cli.bundled.js:37022:16)
at /usr/lib/node_modules/truffle/build/cli.bundled.js:210096:36
at XMLHttpRequest.request.onreadystatechange (/usr/lib/node_modules/truffle/build/cli.bundled.js:208875:13)
at XMLHttpRequestEventTarget.dispatchEvent (/usr/lib/node_modules/truffle/build/cli.bundled.js:210748:18)
at XMLHttpRequest._setReadyState (/usr/lib/node_modules/truffle/build/cli.bundled.js:211038:12)
at XMLHttpRequest._onHttpResponseEnd (/usr/lib/node_modules/truffle/build/cli.bundled.js:211193:12)
at IncomingMessage.<anonymous> (/usr/lib/node_modules/truffle/build/cli.bundled.js:211153:24)
at endReadableNT (_stream_readable.js:1047:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)
Test uses promises like this:
return MyToken.deployed().then(inst => {
instance = inst;
return instance.purchase({from: accounts[1], value: amount});
})....
Problem is that I cannot handle this exception. I tried to wrap purchase
call into function and use assert.throws(executor, message)
- it does not works (suppose due it expects something synchronous).
How this promise can be tested (preferably without adding chai-as-promised
)?
catch
that gets called when the promise fails. See this stackoverflow.com/questions/26571328/… .assert.throws
).