Truffle script: how to run multiple assertions without stopping the program?

I am trying to run multiple assertion using a single script. My script stops after the first assertion: My output is:

t$ truffle exec ts4_for_forum.js
Using network 'development'.
acc2 balance 66998739200000000000 address 0x6b6bD90bFdc16291A9DAa6BDddA78d0Be52184Ba
SC1 deployed 0xA368944966a2B816d63646baeC466764F1Ca0c00
Initial SC1: 0xA368944966a2B816d63646baeC466764F1Ca0c00 balance is 22000000000000000000
Deposited 11 Ether from 0x6b6bD90bFdc16291A9DAa6BDddA78d0Be52184Ba, sc1: 0xA368944966a2B816d63646baeC466764F1Ca0c00 balance is 33000000000000000000
{ AssertionError [ERR_ASSERTION]: Victim balance not same as victim balance before transfer
at module.exports (/home/zulfi/Truffle_programs/transfer_Eth_script/ts4_for_forum.js:41:12)
at process._tickCallback (internal/process/next_tick.js:68:7)
generatedMessage: false,
name: 'AssertionError [ERR_ASSERTION]',
actual: '33000000000000000000',
expected: '0',
operator: '==' }

My script is:

var assert = require('assert');
const MySC1 = artifacts.require("MySC1")
module.exports = async function(callback) {
try {
// Fetch accounts from wallet - these are unlocked
const accounts = await web3.eth.getAccounts()
// Set up account to transferEther to Victim
const acc2 = accounts[2]
acc2bal = await web3.eth.getBalance(acc2)
web3.utils.fromWei(acc2bal, "ether")
console.log('acc2 balance', acc2bal, 'address',acc2)
// Fetch the deployed exchange
const sc1 = await MySC1.deployed()
console.log('SC1 deployed', sc1.address)
sc1bal = await web3.eth.getBalance(sc1.address)
web3.utils.fromWei(sc1bal, "ether")
console.log(`Initial SC1:`,sc1.address,` balance is ${sc1bal}`)
amount = '11'
await web3.eth.sendTransaction({to:sc1.address, from:acc2, value: web3.utils.toWei(amount)})
sc1bal = await web3.eth.getBalance(sc1.address)
web3.utils.fromWei(sc1bal, "ether")
console.log(`Deposited ${amount} Ether from ${acc2}, sc1:`, sc1.address,` balance is ${sc1bal}`)
assert.equal(sc1bal, '0',"Victim balance not same as victim balance before transfer");
assert.equal(sc1bal, '10',"Testing again Victim balance not same as victim balance before transfer");
catch(error) {

My sc is:

pragma solidity ^0.5.8;
contract MySC1 {
   address owner;
   constructor() public {
      owner = msg.sender;
   function sendTo(address payable receiver, uint amount) public {
      (bool success,) ="");
   function() external payable{

I found this link but they have not provided any example of execution and is not related to truffle. Link not related to Truffle and no example


Answers 1

  • To begin with, you are not running your script as an unit test, so I guess it isn't meant to be one. If it's not meant to be a unit test, it probably shouldn't have asserts. You should run unit test with the truffle test functionality.

    Otherwise, in general, as is also said in your link, you should only have one assert per unit test. I personally don't often abide by this rule either, but it's a good goal to keep in mind. If the first assert fails, the second assert is not tested at all, so you won't know whether or not that would succeed. Unit tests (and I guess also script runs) cancel the moment any assertion fails.

    Also, in your case, your two assertions don't make much sense. They are checking if one value is two different values. One of the assertions will always fail, and therefore the script will fail.

Related Questions