I am learning solidity by transcribing code from "Practical Smart Contract Development with Solid and Etherium"(OREILLY).
I am studying hard but struggling along the way.
It is to be tested to see if an error occurs when a non-owner is called.
Can anyone give me some advice?
The error is as follows
Contract: Fundraiser setBeneficiary throws and error when called from a non-owner account: AssertionError: should not be permitted: expected undefined to equal 'Ownable:caller is >not the owner' at Context. (test/fundraiser_test.js:71:16)
test code(trst/fundraiser_test.js).
The last of the following is the relevant code.
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")
}
});
});
});
contruct(contructs/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;
}
}
All codes can be found here.