将以太坊地址添加到动态数组

Add ethereum address to dynamic array

我正在尝试使用 Solidity 将地址动态添加到地址数组。但是,当我 运行 truffle 测试时,我的测试失败了。请看我测试的以下片段:

function testAddPet() public {
  address expected = this;
  address[] storage adopters;
  adopters.push(expected);

  Assert.equal(adopters[0], expected, "Cannot add new pet" );
}

根据我的理解 "address expected" returns 我当前的地址(我 运行ning 在 ganache-cli 的地址 0 上,帐户 1 上有元掩码(不是零索引))。 通过推动这个元素,我的期望是 adopters[0] = expected。

这是我的测试结果:

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)

对我来说,测试结果并没有真正帮助我解决它为什么不起作用的问题。我希望这里有人能理解我不理解的地方 :)。

谢谢!

所以,我的测试成功了。请参阅包括以下解释的代码片段:

function testAddPet() public {
  address expected = address(this);
  address[] storage adopters;
  adopters.push(expected);

  Assert.equal(adopters[0], expected, "Incorrect adopter");
}

正如 Muhammed Altabba 指出的 "this" 应该替换为 "address(this)"。