【问题标题】:Understanding truffle test了解松露测试
【发布时间】:2021-09-15 08:43:51
【问题描述】:

我正在学习使用 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;
}

【问题讨论】:

    标签: solidity truffle web3js


    【解决方案1】:

    这是交易参数。

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

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

    【讨论】:

    • 感谢您链接文档。很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2016-08-06
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多