0

I have a setter function that takes two arguments and makes a simple update of two variables.

uint256 public userAgeA;
uint256 public userAgeB;

function setAge(uint256 _userAgeA, uint256 _userAgeB) public {
    userAgeA = _userAgeA;
    userAgeB = _userAgeB;
}

And then get updated values of them via the getter function.

function getAge() public view returns(uint256, uint256) {
    return (userAgeA, userAgeB);
}

While testing I tried to check if the setter works fine using Chai matches "expect to equal" BUT the main problem is that test works when passing only one argument while passing two arguments gives an assertion BigNumber error.

My testing

it("Should return new ages of userA and userB", async function () {
const GetterSetter = await ethers.getContractFactory("GetterSetter");
const deplCont = await GetterSetter.deploy();
await deplCont.deployed();

const setAgeTx = await deplCont.setAge(16, 28);

// wait until the transaction is mined
await setAgeTx.wait();

expect(await deplCont.getAge()).to.equal(16, 28);});

1 Answer 1

0

Your view function is returning an array of [userAgeA, userAgeB], where both are BigNumbers, so you need to modify the expect statement a bit:

expect(await deplCont.getAge()).to.deep.equal([BigNumber.from(16), BigNumber.from(28)]);
3
  • Actually, its not an array. Just two single variables. Btw, I tried your code but test is still failing.
    – Decode_me
    Jul 14, 2022 at 10:25
  • @Decode_me The problem could be that javascript uses an object BN to wrap the large numbers that ethereum uses, so comparing with a number may not work.
    – Ismael
    Jul 16, 2022 at 23:16
  • This intrigued me so much, I recreated a dummy project to test locally. The object returned is an array, but it's a BigNumbers array, so the [16, 28] need to be transformed into [BigNumber.from(16), BigNumber.from(28)] and the equality check has to be 'deep'. I'll edit the response to the correct one.
    – OneThunder
    Jul 17, 2022 at 19:11

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.