TransWikia.com

Error when testing the contract

Ethereum Asked by neha agarwal on August 26, 2021

ApprovalContract.sol

pragma solidity ^0.5.1;

    contract ApprovalContract{
    address public sender;
    address payable public reciever;
    address public constant approver=0xe05cE3f89b5Ab28d6d2Ac8A503473Cd5A23e616a;

    function deposit (address payable _receiver)external payable{

        require (msg.value > 0);
        sender=msg.sender;
        reciever=_receiver;
    }

    function viewApprover () external pure returns(address) {
        return approver;
    }

    function approve () external payable {

        require (msg.sender==approver);
        reciever.transfer(address(this).balance);
    }
    }

2_deploy_contract.js

const ApprovalContract = artifacts.require("ApprovalContract");

module.exports = function(deployer) {
  deployer.deploy(ApprovalContract);
}

For testing, ApprovalContract.js

const ApprovalContract = artifacts.require('ApprovalContract');

console.log("something");
  contract('ApprovalContract', function(accounts) {

    it('initiates contract', async function() {

      const contract = await ApprovalContract.deployed();
      const approver = await contract.approver.call();
      assert.equal(approver, 0x0feeede47d471276377ed83e3ef749995fc0ebe2, "approvers don't match");
    });
    it('takes a deposit', async function () {
      const contract = await ApprovalContract.deployed();
      await contract.deposit(accounts[0], { value: 1e+18, from: accounts[1] });
      assert.equal(web3.eth.getBalance(contract.address), 1e+18, "amount did not match");
    });
    it('makes the transaction when approved, approver: ' + accounts[2], async function () {
      const contract = await ApprovalContract.deployed();
      await contract.deposit(accounts[0], { value: 1e+18, from: accounts[1] });
      await contract.approve({ from: accounts[2] });
      assert.equal(web3.eth.getBalance(contract.address), 0, "didn't transfer ether");
    });
});

truffle compile,truffle migrate run successfully. When I do truffle test,this error comes-

1) Contract: ApprovalContract
   takes a deposit:
 AssertionError: amount did not match: expected {} to equal 1000000000000000000
  at Context.<anonymous> (test/ApprovalContract.js:15:14)
  at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:189:7)

2) Contract: ApprovalContract

makes the transaction when approved, approver: 0xAD90b2dd2A7E8091349A96e50c4112E1EA0159fE:
 Error: Returned error: VM Exception while processing transaction: revert
  at Object.ErrorResponse (/home/neha/.nvm/versions/node/v8.16.0/lib/node_modules/truffle/build/webpack:/~/web3-core-requestmanager/~/web3-core-helpers/src/errors.js:29:1)

One Answer

You have two problems in your code:

  1. getBalance is async. For it to work you need to wait for the result:

    const balance = await web3.eth.getBalance(contract.address);
    
  2. Js does not have large numbers so web3 wraps them in a BN object. To compare with other numbers is better to compare them as strings since equal do not understand how to compare BN objects and numbers:

    assert.equal(
        balance.toString(),
        web3.utils.toBN(1e+18).toString(),
        "amount did not match"
    );
    

Answered by Ismael on August 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP