I'm writing Unit Test using hardhat for a solidity contract. There is a public mapping variable which has a mapping of address to array of struct.
struct ticket {
uint16 looteryID;
uint ticketPrice;
uint ticketSerialNo;
}
mapping(address => ticket[]) public activeTickets;
function buyLottery(uint16 _id) payable public {
activeTickets[msg.sender].push(ticket(_id,10,100));
}
I want to write unit test where I want to check the length of ticket array in activeTickets for msg.sender (anyone who is calling buyLottery function). I tried few hit and trail but all failed. Below is one of the attempts which failed.
Unit Test Code
it("User can buy lottery",async () => {
await lotteryInstance.connect(addr1).buyLottery(1,{value: ethers.utils.parseEther('1')});
const userTickets = await lotteryInstance.activeTickets(addr1.address);
expect(userTickets.length).equal(1);
expect(userTickets[0].looteryID).equal(1);
});
This will push and ticket details to activeTickets. If we assume msg.sender is 0xFe123d4 then activeTickets should have something like 0xFe123d4 => [{1,10,100}] Now I want to test whether activeTickets was successfully updated or not, by checking current length of ticket w.r.t address 0xFe123d4 and also by accessing and comparing the pushed value.
userTickets