1

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.

https://github.com/okahijiki/fundraiser

1 Answer 1

2

try truffle-assertions

install it: npm install truffle-assertions

require it in your test file:

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

and then use it in your test on the following way

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

in tour case should be something like this:

  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"
      );
  });
1
  • I pasted your code in place of the code in this book and the truffle test passed.Thank you😊 I didnt know truffle-asserions.I would like to study it from now on. If you don't mind my asking for your reference, could you tell me what was wrong with the book code? Personally, 'err.reason',which I could not find on the internet,bother me. const actualError = err.reason;
    – kintoki
    Commented Oct 12, 2022 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.