Testing on Hardhat Simple Storage #27
-
I was trying to write an additional test to see if the contract could add a new person to the array and if the contract's mapping worked as intended: it("Should correctly add a new person with a name and fav num", async function () {
const tx = await simpleStorage.addPerson("Bob", "9");
await tx.wait(1);
const favNumber = 9;
assert.equal(
simpleStorage.nameToFavoriteNumber(simpleStorage.people[0].name),
favNumber
);
}); I am getting this error : TypeError: Cannot read properties of undefined (reading 'name')
at Context.<anonymous> (test/test-deploy.js:38:72)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runNextTicks (node:internal/process/task_queues:65:3)
at listOnTimeout (node:internal/timers:528:9)
at processTimers (node:internal/timers:502:7) Does anyone know how I would correctly format the assert statement to test this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Good question, remember though, in javascript, every smart contract function call is an API call to the smart contract - not the data structure itself. simpleStorage.people[0].name the syntax you’re using here is solidity, but you’re writing in JavaScript! I’m assuming
and get the name from that object! Could you try that? Then try:
And you might find your answer :) |
Beta Was this translation helpful? Give feedback.
Good question, remember though, in javascript, every smart contract function call is an API call to the smart contract - not the data structure itself.
the syntax you’re using here is solidity, but you’re writing in JavaScript!
I’m assuming
people
is your array, so you’d want to do:and get the name from that object! Could you try that?
Then try:
And you might find your answer :)