简体   繁体   中英

Getting error while running test on the solidity code

This is the solidity code, a simple code provided in linkedin learning course -

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.7.0;

contract ApprovalContract{
    address public sender;
    address payable public receiver;
    address public constant approver = 0x9BE32C0CB7910d71CA2c2a7D6B46ebd273dA01eC;
    function deposit(address payable _receiver) external payable{
        require(msg.value>0,"message smaller than 0");
        sender = msg.sender;
        receiver = _receiver;
    }

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

    function approve() external{
        require(msg.sender == approver,"message is approved");
        receiver.transfer(address(this).balance);
    }
}

and this is the test code -

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

contract("ApprovalContract",function (accounts) {
    it('initiates contract',async function(){
        const contract = await ApprovalContract.deployed();
        const approver = await contract.approver.call();
        assert.equal(approver,0x9BE32C0CB7910d71CA2c2a7D6B46ebd273dA01eC,"approvers don't match");
    });
    it('takes a deposit',async function(accounts){
        const contract = await ApprovalContract.deployed();
        await contract.deposit(accounts[0],{value:1e+10,from:accounts[1]});
        assert.equal(web3.eth.getBalance(contract.address),1e+10,"account did not match");
    });
});

Keep getting the error whenever i run the test code using truffle test -

Error: invalid address (arg="_receiver", coderType="address", value=undefined)
Error: invalid address (arg="_receiver", coderType="address", value=undefined)
C:\Users\tanis\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\runner.js:726
  err.uncaught = true;
               ^

TypeError: Cannot create property 'uncaught' on string 'abort(Error: invalid address (arg="_receiver", coderType="address", value=undefined)). Build with -s ASSERTIONS=1 for more info.'

I learned the same course and encountered the same problem. it ended up working for me (but not sure exactly what the problem was).

Try connecting a meta mask like this:

 ethereum.request({ method: 'eth_requestAccounts' });

also try several different sending and receiving addresses

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM