dappuniversity / token_sale

Code Your Own Cryptocurrency & ICO on Ethereum | Tutorial
406 stars 387 forks source link

Update for test/DappTokenSale.js #42

Open sudiptab2100 opened 3 years ago

sudiptab2100 commented 3 years ago

"error.message.indexOf('revert')" is deprecated now. "error.toString().indexOf('revert')" can be used instead.

rajeebkm commented 2 years ago

Follow the Transfer function and test cases below to get solution.

//Transfer function transfer(address _to, uint256 _value) public returns(bool success){ //Exception if account doen't have enough value require(balanceOf[msg.sender] >= _value, "Sender doesn't have sufficient balance"); balanceOf[msg.sender] -= _value; balanceOf[_to] += _value; //Trigers Transfer event emit Transfer(msg.sender, _to, _value); //Returns a boolean return true; }

//Test case

-------------------------Passing larger amount----------------------------------------------

it('transfers token ownership', function() {
    return RAJToken.deployed().then(function(instance) {
      tokenInstance = instance;
      // Test `require` statement first by transferring something larger than the sender's balance
      return tokenInstance.transfer.call(accounts[1], 99999999999999999999999);
    }).then(assert.fail).catch(function(error) {
      console.log(error.message); //To know the error message through console.log
      assert(error.message.toString().includes('overflow'), 'error message must contain overflow');
      return tokenInstance.transfer.call(accounts[1], 250000, { from: accounts[0] });
    }).then(function(success) {
      assert.equal(success, true, 'it returns true');
      return tokenInstance.transfer(accounts[1], 250000, { from: accounts[0] });
    }).then(function(receipt) {
      assert.equal(receipt.logs.length, 1, 'triggers one event');
      assert.equal(receipt.logs[0].event, 'Transfer', 'should be the "Transfer" event');
      assert.equal(receipt.logs[0].args._from, accounts[0], 'logs the account the tokens are transferred from');
      assert.equal(receipt.logs[0].args._to, accounts[1], 'logs the account the tokens are transferred to');
      assert.equal(receipt.logs[0].args._value, 250000, 'logs the transfer amount');
      return tokenInstance.balanceOf(accounts[1]);
    }).then(function(balance) {
      assert.equal(balance.toNumber(), 250000, 'adds the amount to the receiving account');
      return tokenInstance.balanceOf(accounts[0]);
    }).then(function(balance) {
      assert.equal(balance.toNumber(), 750000, 'deducts the amount from the sending account');
    });
  });

--------------------------------------------Output----------------------------------------------------- Contract: RAJToken ✓ initializes the contract with correct values (112ms) ✓ allocates the initial supply upon deployment (100ms) overflow (fault="overflow", operation="BigNumber.from", value=1e+23, code=NUMERIC_FAULT, version=bignumber/5.0.8) ✓ transfers token ownership (2750ms)

PS: If error.message contains overflow word as you can see above in output, it will pass the test case. That means if you send larger value than your balance, it will result in overflow. so if result contains overflow word that we are asserting, that means it will pass the test case.

----------------------Passing lesser value-------------------------------

it('transfers token ownership', function() { return RAJToken.deployed().then(function(instance) { tokenInstance = instance; // Test require statement first by transferring something larger than the sender's balance return tokenInstance.transfer.call(accounts[1], 1); }).then(assert.fail).catch(function(error) { console.log(error.message); assert(error.message.toString().includes('overflow'), 'error message must contain overflow'); return tokenInstance.transfer.call(accounts[1], 250000, { from: accounts[0] }); }).then(function(success) { assert.equal(success, true, 'it returns true'); return tokenInstance.transfer(accounts[1], 250000, { from: accounts[0] }); }).then(function(receipt) { assert.equal(receipt.logs.length, 1, 'triggers one event'); assert.equal(receipt.logs[0].event, 'Transfer', 'should be the "Transfer" event'); assert.equal(receipt.logs[0].args._from, accounts[0], 'logs the account the tokens are transferred from'); assert.equal(receipt.logs[0].args._to, accounts[1], 'logs the account the tokens are transferred to'); assert.equal(receipt.logs[0].args._value, 250000, 'logs the transfer amount'); return tokenInstance.balanceOf(accounts[1]); }).then(function(balance) { assert.equal(balance.toNumber(), 250000, 'adds the amount to the receiving account'); return tokenInstance.balanceOf(accounts[0]); }).then(function(balance) { assert.equal(balance.toNumber(), 750000, 'deducts the amount from the sending account'); }); }); --------------------------------------Output--------------------------------------- Contract: RAJToken ✓ initializes the contract with correct values (68ms) ✓ allocates the initial supply upon deployment (150ms) true 1) transfers token ownership

No events were emitted

2 passing (497ms) 1 failing

1) Contract: RAJToken transfers token ownership: AssertionError: error message must contain overflow

PS: If error.message doesn't contain overflow word as you can see above in output, it willn't pass the test case. That means if you send lesser value than your balance, it (error.message) will result true. so the result doesn't contain overflow word. Our assertion is false,so it will give the error message (AssertionError: error message must contain overflow). Test case willn't pass.

--------------------Final resolve solution----------------------

it('transfers token ownership', function() { return RAJToken.deployed().then(function(instance) { tokenInstance = instance; // Test require statement first by transferring something larger than the sender's balance return tokenInstance.transfer.call(accounts[1], 9999999999999999); }).then(assert.fail).catch(function(error) { assert(error.message.toString().includes('overflow'), 'error message must contain overflow'); return tokenInstance.transfer.call(accounts[1], 250000, { from: accounts[0] }); }).then(function(success) { assert.equal(success, true, 'it returns true'); return tokenInstance.transfer(accounts[1], 250000, { from: accounts[0] }); }).then(function(receipt) { assert.equal(receipt.logs.length, 1, 'triggers one event'); assert.equal(receipt.logs[0].event, 'Transfer', 'should be the "Transfer" event'); assert.equal(receipt.logs[0].args._from, accounts[0], 'logs the account the tokens are transferred from'); assert.equal(receipt.logs[0].args._to, accounts[1], 'logs the account the tokens are transferred to'); assert.equal(receipt.logs[0].args._value, 250000, 'logs the transfer amount'); return tokenInstance.balanceOf(accounts[1]); }).then(function(balance) { assert.equal(balance.toNumber(), 250000, 'adds the amount to the receiving account'); return tokenInstance.balanceOf(accounts[0]); }).then(function(balance) { assert.equal(balance.toNumber(), 750000, 'deducts the amount from the sending account'); }); });

------------------Output-------------------------

Contract: RAJToken ✓ initializes the contract with correct values (67ms) ✓ allocates the initial supply upon deployment (106ms) ✓ transfers token ownership (2818ms)

3 passing (3s)

Suggestions: Use this: assert(error.message.toString().indexOf('overflow') >=0, 'error message must contain overflow'); instead of assert(error.message.toString().indexOf('revert') >=0, 'error message must contain revert'); and assert(error.message.indexOf('revert') >=0, 'error message must contain revert'); as error.message doesn't contain 'revert' word, it will always give "-1".

I hope, you can understand from this and use this code for solution. It will work.

Thanks