Solidity

關於 try-catch 錯誤(Truffle Test)

  • October 12, 2022

我通過轉錄“使用 Solid 和 Etherium 進行實用智能合約開發”(OREILLY)的程式碼來學習 Solidity。

我正在努力學習,但一路走來都很艱難。

在呼叫非所有者時,將對其進行測試以查看是否發生錯誤。

誰能給我一些建議?

錯誤如下

契約:從非所有者帳戶呼叫時,籌款人 setBeneficiary 拋出和錯誤:AssertionError:不應該被允許:預期未定義等於 ‘Ownable:caller is >not the owner’ at Context。(測試/fundraiser_test.js:71:16)

測試程式碼(trst/fundraiser_test.js)。

下面最後一個是相關程式碼。

const FundraiserContract = artifacts.require("Fundraiser");

contract("Fundraiser", accounts => {
let fundraiser;
const name =  "Beneficiary Name";
const url = "[beneficiaryname.org](http://beneficiaryname.org/)";
const imageURL = "[https://placekitten.com/600/350] 
(https://placekitten.com/600/350)";
const description = "Beneficiary description";
const beneficiary = accounts[1];
const owner = accounts[0];

beforeEach(async () => {
fundraiser = await FundraiserContract.new(
name,
url,
imageURL,
description,
beneficiary,
owner
)
});

describe("initialization", () => {
it("gets the beneficiary name", async () => {
const actual = await [fundraiser.name](http://fundraiser.name/)();
assert.equal(actual, name, "names should match");
});

it("gets the beneficiary url", async () => {
const actual = await fundraiser.url();
assert.equal(actual, url, "url should match");
});

it("gets the beneficiary image url", async () => {
const actual = await fundraiser.imageURL();
assert.equal(actual, imageURL, "imageURL should match");
});

it("gets the beneficiary description", async () => {
const actual = await fundraiser.description();
assert.equal(actual, description, "description should match");
});

it("gets the beneficiary", async () => {
const actual = await fundraiser.beneficiary();
assert.equal(actual, beneficiary, "beneficiary addresses should match");
});

it("gets the owner", async () => {
const actual = await fundraiser.owner();
assert.equal(actual, owner, "bios should match");
});

});

describe("setBeneficiary", () => {
const newBeneficiary = accounts[2];

it("updated beneficiary when called by owner account", async () => {
await fundraiser.setBeneficiary(newBeneficiary, {from: owner});
const actualBeneficiary = await fundraiser.beneficiary();
assert.equal(actualBeneficiary, newBeneficiary, "beneficiaries should match");
});

//Error-Related Codes

it("throws and error when called from a non-owner 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")
}
});

});
});

結構(結構/Fundraiser.sol)

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";

contract Fundraiser is Ownable {

string public name;
string public url;
string public imageURL;
string public description;
address payable public beneficiary;

constructor(
string memory _name,
string memory _url,
string memory _imageURL,
string memory _description,
address payable _beneficiary,
address _custodian
)
public
{
name = _name;
url = _url;
imageURL = _imageURL;
description = _description;
beneficiary = _beneficiary;
transferOwnership(_custodian);
}

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

所有程式碼都可以在這裡找到。

https://github.com/okahijiki/fundraiser

嘗試松露斷言

安裝它: npm install tr​​uffle-assertions

在您的測試文件中要求它:

const truffleAssert = require('truffle-assertions');

然後通過以下方式在您的測試中使用它

await truffleAssert.fails(
 contractInstance.methodThatShouldFail(),
 truffleAssert.ErrorType.REVERT,
 "Ownable: caller is not the owner"
);

在旅遊案例中應該是這樣的:

 it("throws and error when called from a non-owner account", async () => {
   await truffleAssert.fails(
     fundraiser.setBeneficiary(newBeneficiary, { from: accounts[3]}),
       truffleAssert.ErrorType.REVERT,
       "Ownable: caller is not the owner"
     );
 });

引用自:https://ethereum.stackexchange.com/questions/137326