Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for test/DappTokenSale.js #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Update for test/DappTokenSale.js #42

wants to merge 1 commit into from

Conversation

sudiptab2100
Copy link

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

"error.message.indexOf('revert')" is deprecated now. 
"error.toString().indexOf('revert')" can be used instead.
@sudiptab2100 sudiptab2100 changed the title Minor fix for error messages Update for test/DappTokenSale.js Mar 14, 2021
@rajeebkm
Copy link

rajeebkm commented Aug 19, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants