Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ contract Box is Ownable {

event ValueChanged(uint256 value);

constructor(address initialOwner) Ownable(initialOwner) {}

// The onlyOwner modifier restricts who can call the store function
function store(uint256 value) public onlyOwner {
_value = value;
Expand Down
11 changes: 7 additions & 4 deletions components/learn/modules/ROOT/pages/writing-automated-tests.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ const { expect } = require('chai');

// Start test block
describe('Box', function () {
let owner;

before(async function () {
this.Box = await ethers.getContractFactory('Box');
});

beforeEach(async function () {
this.box = await this.Box.deploy();
[owner] = await ethers.getSigners()
this.box = await this.Box.deploy(owner.address);
await this.box.deployed();
});

Expand Down Expand Up @@ -192,14 +195,14 @@ contract('Box', function ([ owner, other ]) {
const value = new BN('42');

beforeEach(async function () {
this.box = await Box.new({ from: owner });
this.box = await Box.new(owner, { from: owner });
});

it('retrieve returns a value previously stored', async function () {
await this.box.store(value, { from: owner });

// Use large integer comparisons
expect(await this.box.retrieve()).to.be.bignumber.equal(value);
expect((await this.box.retrieve()).toString()).to.equal(value.toString());
});

it('store emits an event', async function () {
Expand All @@ -213,7 +216,7 @@ contract('Box', function ([ owner, other ]) {
// Test a transaction reverts
await expectRevert(
this.box.store(value, { from: other }),
'Ownable: caller is not the owner',
'OwnableUnauthorizedAccount("' + other + '")',
);
});
});
Expand Down