了解松露测试

Understanding truffle test

我正在学习使用 truffle 创建智能合约的教程。我想对教程中创建的测试有更好的理解。

这是测试之一:

it("increases myDonationsCount", async () => {
  const currentDonationsCount = await fundraiser.myDonationsCount(
    {from: donor}
  );

  await fundraiser.donate({from: donor, value});

  const newDonationsCount = await fundraiser.myDonationsCount(
    {from: donor}
  );

  assert.equal(
    1,
    newDonationsCount - currentDonationsCount,
    "myDonationsCount should increment by 1");
})

这个对象来自哪里? {from: donor, value}

对于此测试:

it("throws an error when called from a different account", async () =>{
  try {
    await fundraiser.setBeneficiary(newBeneficiary, {from: accounts[3]});
    assert.fail("withdraw was not restricted to owners")
  } catch(err) {
    const expectedError = "Ownable: caller is not the owner"
    const actualError = err.reason;
    assert.equal(actualError, expectedError, "should not be permitted")
  }
})

在上述测试的第 3 行中,他们传递了 2 个参数 fundraiser.setBeneficiary(newBeneficiary, {from: accounts[3]});。 如果原始函数只接收一个,这怎么可能?

函数:

function setBeneficiary(address payable _beneficiary) public onlyOwner {
    beneficiary = _beneficiary;
}

这是交易参数。

Truffle 允许您在传递了 Solidity 函数中定义的所有参数后,通过在最后一个参数中指定覆盖来覆盖默认交易参数。

文档:https://www.trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts#making-a-transaction