solidity - 了解松露测试
问题描述
我正在按照教程使用松露创建智能合约。我想更好地了解本教程中创建的测试。
这是其中一项测试:
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;
}
解决方案
这是交易参数。
在传递了 Solidity 函数中定义的所有参数后,Truffle 允许您通过在最后一个参数中指定覆盖来覆盖默认事务参数。
推荐阅读
- lorawan - 如何生成 LoRa App 密钥和 App EUI
- php - Zoho Sign Webhook - 使用 PHP 接收响应
- android - 如何在适配器中使用 setSupportActionBar(toolbar)?
- linux - Linux内核调用图构建与分析
- firebase - 如何在 Firestore 规则中检测阵列条目删除请求
- ethereum - 是否有获取 ERC20 令牌标签名称的 api 方法
- data-structures - 嵌套循环最有效的数据结构?
- java - 无法将 json 测试数据文件读入我的 stepDef
- python - 代码在字符串拆分中未按预期工作
- php - 我试图将数据添加到数据库中的用户表中