How to test people's array from SimpleStorage contract ? #199
-
Hi guys, im trying to test the SimpleStorage contract by adding person, favoriteNum it("Should update peoples array", async function () {
const person1 = "harry";
const favoriteNum = "13";
const transactionResponse = await simpleStorage.addPerson(
person1,
favoriteNum
);
await transactionResponse.wait(1);
let { num, currperson1 } = await simpleStorage.people(0);
// assert.equal(num, favoriteNum);
// assert.equal(currperson1, person1);
const currfavoritNum = await simpleStorage.nameToFavoriteNumber("harry");
assert.equal(currfavoritNum.toString(), favoriteNum);
console.log(`haryy logging : ${currperson1}`);
}); But when im trying to read people[0] like this , both num, currperson1 are not getting assinged anything and when i try to print they are undefined , so my assert is getting failed
I have tried different ways to print people(0) in the hardhat console , im not able to separate string, int from people's array. How can i catch that people struct in my code , so that i can test the data in it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I tested like this
|
Beta Was this translation helpful? Give feedback.
-
You need to "pull out" the values from the struct with the same name as what's in the struct. The struct looks like this: struct People {
uint256 favoriteNumber;
string name;
} So to get those variables, you have to use the same names: let { favoriteNumber, name } = await simpleStorage.people(0);
assert.equal(name, "Balajee") Or, the second way you could do it: const person = await simpleStorage.people(0)
assert.equal(person.name, "Balajee") |
Beta Was this translation helpful? Give feedback.
-
Hey Patrick, how am I supposed to compare arrays? I'm getting this error when I do that
|
Beta Was this translation helpful? Give feedback.
You need to "pull out" the values from the struct with the same name as what's in the struct. The struct looks like this:
So to get those variables, you have to use the same names:
Or, the second way you could do it: