0

I want to update public variable abc to 2.

contract ABC {

    uint public abc = 1;

    function updateABC() public {
        abc = 2;
    }
}

And want to write Unit test to check if it was updated to 2 or not.

it("Should be updated to 2", async () => {
            const {ownerInstance} = await loadFixture(deployOwnerControlsFixture);
            ownerInstance.connect.abc = 2;
            expect(ownerInstance.abc()).equals(2);
 });

But this is not working, its throwing error [![enter image description here][1]][1]

If I'm trying something like this

ownerInstance.connect(owner).abc = 2;
let aa = await ownerInstance.abc();
console.log("Updated value of abc",aa);
expect(await ownerInstance.abc()).equal(3);

I'm seeing error[![enter image description here][2]][2]

We can see clearly the value of abc is not updating to 2. [1]: https://i.stack.imgur.com/RZhTc.png [2]: https://i.stack.imgur.com/83jxD.png

2 Answers 2

0

To update a public uint variable you need to call the function that updates the variable - in your case, you can call the updateABC() function to update the abc variable to 2. We also await the ownerInstance as its an async function. Then also check if the variable got updated with to.equal(2).

Code

it("Should be updated to 2", async () => {
    const { ownerInstance } = await loadFixture(deployOwnerControlsFixture);
    await ownerInstance.updateABC();
    expect(await ownerInstance.abc()).to.equal(2);
});
1
  • Thanks for the reply @timsntech, what if I want to assign 3 to variable abc, we can definitely pass through function parameters and then can update abc and then we can test that, but I want this without calling updateABC() function. code it("Should be updated to 3", async () => { const {ownerInstance} = await loadFixture(deployOwnerControlsFixture); ownerInstance.connect.abc = 3; expect(ownerInstance.abc()).equals(3); }); Mar 6, 2023 at 2:29
0

The error says you are comparing BigNum and Num. Try this:

expect(ownerInstance.abc().toNumber()).equals(2);
1
  • now its showing TypeError: ownerInstance.abc(...).toNumber is not a function. Mar 6, 2023 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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