Solidity testing of the Struct

Viewed 1044

I have tested smart contract (hardhat). I set parameters by setter function and compare result with getter function. The data have mapping and struct structures below:

struct Candidate {
    address candidateAddress; 
    uint voteCount; //ammont of votes for candidate
}

struct VotingCampaign {
    bool isVotingCampaignEnded; // is voting compaign has been ended?
    Candidate[] candidates; //array of candidates
    uint poolToWin; //ETH pool to win
    uint commission; //contract owner commision
    mapping(address => bool) votersVoted; //mapping if voter has already voted
    address winningAddress;
    uint loocUpTime; //the end of lock up period - 3 days
}

uint campaignId; //voting compaing sequence number
mapping (uint => VotingCampaign) campaigns; //mapping of all campaigns.

The setter:

    function addVoting(address[] memory _candidateAddress) public isOwner { //adding new voting in blockchain
    VotingCampaign storage compaign = campaigns[campaignId]; 
    for (uint i = 0; i < _candidateAddress.length; i++) { //adding array of candidates by loop from function arguments to blockchain Struct storage.
        compaign.candidates.push(Candidate({
            candidateAddress: _candidateAddress[i],
            voteCount: 0
        }));
    }
    compaign.loocUpTime = block.timestamp + 3 days; //calculate the period voting can't be finished
    emit NewCampaignAdded(campaignId);
    campaignId++; //counter increase
}

getter:

function viewCompaing(uint _campaignId) public view returns(Candidate[] memory, address) {
    return (campaigns[_campaignId].candidates, campaigns[_campaignId].winningAddress); //results getter
}

How to write to.equal.parameters? thank you!

my test Code:

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Voting", function () {
let voting;

beforeEach(async ()=>{
  [owner, addr1, addr2,addr3, addr4, ...addrs] = await ethers.getSigners();
  const Voting = await ethers.getContractFactory("Voting");
  voting = await Voting.deploy();
  await voting.deployed();
  
  /*const tx = {
    to: voting.vote(0,1),
    value: ethers.utils.parseEther('1')
  }
  const sendTx = await addr4.sendTransaction(tx);
  await sendTx.wait();*/
  
 });

it("Checking owner is changed", async function () {
await voting.changeOwner(addr1.address);
expect(await voting.owner()).to.equal(addr1.address);
});

it("Checking additing new candidate list", async function (){
await voting.addVoting([addr2.address, addr3.address]);
console.log(await voting.viewCompaing(0));
const [candidates, winner] = await voting.viewCompaing(0);
const[cand1, cand2] = candidates;

const [ca1, ca2] = cand1;
expect(await ca2).to.be.equal("0");
expect(await ca1).to.be.equal("0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC");
expect(candidates).to.equal([
  ['0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC', ethers.BigNumber.from("0")],
  ['0x90F79bf6EB2c4f870365E785982E1f101E93b906', ethers.BigNumber.from("0")]]);
});
});

When I make testing I get the next error on the picture new one

1 Answers

You are comparing a string with a ethers.BigNumber. First, import BigNumber if you haven't yet. It is a utility library and part ethers.js.

See BigNumber for more information.

Then, convert your "0" to a BigNumber and compare it with the result in your test.

expect(...).to.equal([...["address", ethers.BigNumber.from("0")]...])

You should look into js/ts more, because you are not comparing correct values. And also avoid using constant addresses, ethersjs provides signers for you.

To be more exact, here is the corrected test case for you:


        const [candidates, winner] = await voting.viewCompaing(0);
        const [cand1, cand2] = candidates;

        expect(cand1.candidateAddress).to.be.equal(addr2.address);
        expect(cand2.candidateAddress).to.be.equal(addr3.address);

        expect(cand1.voteCount).to.be.equal(ethers.BigNumber.from("0"));
        expect(cand2.voteCount).to.be.equal(ethers.BigNumber.from("0"));

        expect(winner).to.be.equal(ethers.constants.AddressZero);
Related