TestUtils.js
1 2 // This has been tested with the real Ethereum network and Testrpc. 3 // Copied and edited from: https://gist.github.com/xavierlepretre/d5583222fde52ddfbc58b7cfa0d2d0a9 4 exports.assertReverts = (contractMethodCall, maxGasAvailable) => { 5 return new Promise((resolve, reject) => { 6 try { 7 resolve(contractMethodCall()) 8 } catch (error) { 9 reject(error) 10 } 11 }) 12 .then(tx => { 13 assert.equal(tx.receipt.gasUsed, maxGasAvailable, "tx successful, the max gas available was not consumed") 14 }) 15 .catch(error => { 16 if ((error + "").indexOf("invalid opcode") < 0 && (error + "").indexOf("out of gas") < 0) { 17 // Checks if the error is from TestRpc. If it is then ignore it. 18 // Otherwise relay/throw the error produced by the above assertion. 19 // Note that no error is thrown when using a real Ethereum network AND the assertion above is true. 20 throw error 21 } 22 }) 23 } 24 25 exports.listenForEvent = event => new Promise((resolve, reject) => { 26 event.watch((error, response) => { 27 if (!error) { 28 resolve(response.args) 29 } else { 30 reject(error) 31 } 32 event.stopWatching() 33 }) 34 })