2

I am trying to run the unit test below and getting the following error:

AssertionError [ERR_ASSERTION]: '0x4ede706B3Fa847c1355D88a64b5F5DB8F6Bd9b6a' == undefined

Here is my unit test:

it('rights given to account', async () => {
    await fantasy.methods.giveRightToVote({ from: accounts[0], to: accounts[1] })

    const getRights = await fantasy.methods.getRightsToVote().call({ from : accounts[0] });

    assert.equal(accounts[1], getRights[0]);
    assert.equal(1, getRights.length);

Here is my actual smart contract:

pragma solidity ^0.4.17;

contract FantasyLeague {
    // Define address of commissioner
    address public commissioner;
    // Hold votes
    uint[] public proposals;
    // Hold addresses of league members
    address[] public members;
    // Hold addresses of those given votings rights to compare to those who have voted
    address[] public rightsGiven;

    // Deploy contract
    constructor() public payable {
        commissioner = msg.sender;
    }

    function giveRightToVote(address voter) public {
        require(msg.sender == commissioner);
        // Ensures that the right to vote given matches the actual voted addresses
        rightsGiven.push(voter);
    }

    // Propose new team number
    function vote (uint number) public payable {
        // Require ether to reduce fraud
        require(msg.value > .01 ether);
        proposals.push(number);
        members.push(msg.sender);
    }

    // Retrieve List of current proposals
    function getVotes () public view returns (uint[]) {
        return proposals;
    }

    // Retrieve memebrs that have been given right to vote
    function getRightsToVote () public view returns (address[]) {
        return rightsGiven;
    }

    // This function will track the address for every vote cast.  
    //If there are two votes from the same address or a vote from an address not given a right to vote, you know it is invalid
    function getMembers () public view returns (address[]) {
        return members;
    }
}
  • If you attach code of smart contract then it will be helpful to understand what the exact problem is – Soham Lawar Sep 7 '18 at 13:54
  • Yeah, at least the text of the getRightsToVote() function would be helpful here. All I can say from that assertion error is that getRights is an object or array. – ohsully Sep 7 '18 at 14:23
  • For sure. Below is the part of the .sol file. I am constrained by character limit to put the whole thing, but please let me know if you need more context. What I am not sure I understand working with web3 is when accessing the contract do you use the contracts native functions, variables etc or make your own in the .test file? – kefa Sep 8 '18 at 15:07
  • @ohsully any more feedback now that you have the smart contract below? Thanks for your help thus far! – kefa Sep 12 '18 at 21:52
  • I was able to figure it out. I needed to pass the right arguments into the giveRightToVote().send() method. Thank you all for your help! – kefa Sep 14 '18 at 12:27
2

I am not sure, but shouldn't you pass the argument before {from:} object:

await fantasy.methods.giveRightToVote(accounts[1],{ from: accounts[0])

  • My answer misunderstood where your issue was coming from, I think @desimira is on point -- that's definitely a bug, and it would lead to an undefined value being pushed into that array. – ohsully Sep 13 '18 at 7:12
1

I am not 100% sure however can you define getRights outside the "it" testing function?

let getRights;

it('rights given to account', async () => { await fantasy.methods.giveRightToVote({ from: accounts[0], to: accounts[1] })

getRights = await fantasy.methods.getRightsToVote().call({ from : accounts[0] });

assert.equal(accounts[1], getRights[0]);
assert.equal(1, getRights.length);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.