Complete the code to import the Hardhat testing helpers.
const { [1] } = require("hardhat");The ethers object is imported from Hardhat to interact with the Ethereum blockchain in tests.
Complete the code to get the list of signers for testing.
const [owner, addr1] = await [1].getSigners();The ethers object provides the getSigners() method to get test accounts.
Fix the error in deploying the contract instance.
const Contract = await ethers.getContractFactory("MyContract"); const contract = await Contract.[1]();
The deploy() method creates a new contract instance on the blockchain.
Fill both blanks to write a test that checks the contract owner.
describe("MyContract", function () { it("should set the right owner", async function () { const [[1]] = await ethers.getSigners(); const Contract = await ethers.getContractFactory("MyContract"); const contract = await Contract.deploy(); await contract.deployed(); expect(await contract.owner()).to.equal([2].address); }); });
The first signer is usually called owner, and the test checks if the contract's owner matches owner.address.
Fill all three blanks to write a test that checks a function revert with a custom error message.
it("should revert if not owner", async function () { const [owner, [1]] = await ethers.getSigners(); const Contract = await ethers.getContractFactory("MyContract"); const contract = await Contract.deploy(); await contract.deployed(); await expect(contract.connect([2]).restrictedFunction()).to.be.revertedWith([3]); });
The test uses addr1 as a non-owner signer and expects the revert message "Ownable: caller is not the owner".