0

I am currently reading 「Hands-On Smart Contract Development with Solidity and Ethereum」(O’REILLY)and transcribing the code in this book.

I am getting errors and my hands are tied.

The errors are as follows.

ReferenceError: accounts is not defined (/Users/tsuru/fundraiser/test/fundraiser_test.js:59:28)

According to the error, there seems to be a problem with      

const newBeneficiary = accounts[2];

The code is here.

/fundraiser/test/fundraiser_test

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();
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", () => {

//The line where the error is pointed out
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");
});


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")
 }
 })
 })

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

1

You are closing contract parentheses early after the beforeEach hook. therefore, you have less closing parentheses at the end. this should work:

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

contract("Fundraiser", (accounts) => {
  let fundraiser;
  const name = "Beneficiary Name";
  const url = "[beneficiaryname.org](http://beneficiaryname.org/)";
  // I do not know what kind of syntax is this
  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();
      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", () => {
    //The line where the error is pointed out
    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"
      );
    });

    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");
      }
    });
  });
});
2
  • I was able to clear up the problem. I just thought it was an accounts issue. I had not thought that the closing of the parentheses was the cause.thank you :)
    – kintoki
    Commented Oct 11, 2022 at 1:34
  • @kintoki did it work?
    – Yilmaz
    Commented Oct 11, 2022 at 1:48

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.