作为不同的用户测试 solidity 合约

testing solidity contracts as a different user

我推断可靠性测试将作为一个特定的 user/account 隐含地进行。例如在这样的测试中:

  const instance = await MyCoin.deployed()
  const accountTwo = accounts[1]
  const amount = 100
  await instance.transfer(accountTwo, amount)

那么转帐将从帐户[0] 到帐户[1],这意味着进行转帐的user/account 负责帐户[0]。

有什么方法可以明确 and/or 更改吗?因此,例如,我们可以编写一个测试,作为负责其他帐户的用户运行吗?

我一直在阅读 https://www.trufflesuite.com/docs/truffle/testing/writing-tests-in-javascript and https://remix-ide.readthedocs.io/en/latest/unittesting_examples.html 但我不确定我是否看到任何关于如何 select 特定用户作为

操作的信息

https://remix-ide.readthedocs.io/en/latest/unittesting_examples.html#testing-a-method-involving-msg-sender 有一个特殊的合同,其中包含更改合同所有者变量的操作,但我认为这不是我想要的...

您可以在函数的最后一个参数中定义交易发送方(即执行函数的账户)。它也被称为“交易参数”。如果未定义(如您的情况),Truffle 将使用默认值。

此代码段从 accountThree 执行 transfer(accountTwo, amount)。前两个参数是 Solidity 函数的参数,第三个包含交易参数。

await instance.transfer(accountTwo, amount, {from: accountThree})

您可以在 docs 中找到更多信息。