首页 > 解决方案 > 了解松露测试

问题描述

我正在按照教程使用松露创建智能合约。我想更好地了解本教程中创建的测试。

这是其中一项测试:

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")
  }
})

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

功能:

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

标签: soliditytruffleweb3js

解决方案


这是交易参数。

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

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


推荐阅读