Skill 6.2.2
Test reverts, access control and ether transfers
Key Concepts
Chai matchers check failures: await expect(tx).to.be.revertedWithCustomError(contract, "NotOwner") for custom errors (optionally with .withArgs(...)), .revertedWith("message") for revert strings, .revertedWithPanic() for assert failures and arithmetic panics, and .revert(ethers) for any revert (it replaces the deprecated .reverted of older Hardhat versions). In Foundry and Hardhat Solidity tests, vm.expectRevert() is called before the failing call.
Access control is tested by calling privileged functions from an account without permission: contract.connect(attacker).mint(...) must revert, while the same call from the owner succeeds. Ether and token movements are checked with changeEtherBalances(ethers, [a, b], [-x, x]) and changeTokenBalances(ethers, token, [a, b], [-x, x]), which compare balances before and after the transaction; ether balance checks can ignore gas fees paid by the sender.