Lesson 6: test gives unexpected result #497
-
My test is giving me the wrong result. beforeEach(async () => {
simpleStorageFactory = await ethers.getContractFactory("SimpleStorage");
simpleStorage = await simpleStorageFactory.deploy();
await simpleStorage.deployed();
});
it.only("people's length should be 2 after adding two people", async () => {
await simpleStorage.addPerson("Blessing", "22");
await simpleStorage.addPerson("Over", "22");
let length = await simpleStorage.people.length;
assert.equal(length, 2);
}); Test error:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The length of a public array is not exposed by default in solidity. First way(easy to implement) : And call that function in your test : You can refer to this : The second way(complex) : |
Beta Was this translation helpful? Give feedback.
-
@Olaleye-Blessing |
Beta Was this translation helpful? Give feedback.
The length of a public array is not exposed by default in solidity.
First way(easy to implement) :
Define a specific solidity function for it :
And call that function in your test :
You can refer to this :
contract : SimpleStorage.sol
test : test-deploy.js
both the function and required test are written at end of the file respectively.
The second way(complex) :
From ethers.js, read the storage slot of your array. About which you will learn in the further module. The first way is easy and recommended because in the second way when you update your contract you have to update the test again.