I am trying to add an address dynamically to an array of addresses with Solidity. However, when I run truffle test my test fails. Please see the following snippit of my test:
function testAddPet() public {
address expected = this;
address[] storage adopters;
adopters.push(expected);
Assert.equal(adopters[0], expected, "Cannot add new pet" );
}
From my understanding "address expected" returns my current address (I am running on address 0 of ganache-cli, with metamask on account 1 (not zero indexed)). By pushing this element my expectations are that adopters[0] = expected.
This is my test result:
TestAdoption
✓ testUserCanAdoptPet (43ms)
✓ testGetAdopterAddressByPetId (56ms)
✓ testGetAdopterAddressByPetIdInArray (70ms)
1) testAddPet
Events emitted during test:
---------------------------
TestEvent(result: <indexed>, message: Cannot add new pet)
---------------------------
3 passing (590ms)
1 failing
1) TestAdoption testAddPet:
Error: Cannot add new pet
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:319138:17
at Array.forEach (<anonymous>)
at processResult (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:319136:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
For me the test result is not really helping me why it is not working. I hope someone here understand what I do not :).
Thank you!
So, my test works. See the snippit including explanation below:
function testAddPet() public {
address expected = address(this);
address[] storage adopters;
adopters.push(expected);
Assert.equal(adopters[0], expected, "Incorrect adopter");
}
As Muhammed Altabba points out "this" should be replaced to "address(this)".