Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example of "one contract per method test structure" defined in best practices #828

Open
mds1 opened this issue Feb 21, 2023 · 0 comments
Labels
good first issue Good for newcomers T-enhancement Type: enhance existing docs

Comments

@mds1
Copy link
Contributor

mds1 commented Feb 21, 2023

This applies to "General Test Guidance" section 3.1, add something like the following:


Below is an example of this pattern for a factory contract called MyFactory that deploys a child contract called Child. A sample, unimplemented MyFactory contract is below. The actual content of all methods is left out for brevity, and only function signatures and empty function bodies are shown.

// file: src/MyFactory.sol
contract MyFactory {
  constructor() {}

  function deploy(address owner) external {}

  function computeAddress(address owner) external {}
}

Next we have it's test contract. Aside from the MyFactoryTest setup contract, all contract names match methods in the MyFactory contract in the order they appear, and each test function in the test contracts asserts on a single thing that function should do.

// file: test/MyFactory.t.sol
contract MyFactoryTest is Test {
  // Setup to deploy contracts, etc. goes in this contract.
}

contract Constructor is MyFactoryTest {
  function test_SetsChildContractLogicAddress() public {}
  function test_SetsOwnerAddress() public {}
}

contract Deploy is MyFactoryTest {
  function deployChild() internal returns (address) {} // helper method
  function test_RevertsIf_CalledByAccountThatIsNotOwner(address caller) public {}
  function test_IncrementsChildDeployCountByOne() public {}
  function test_DeploysChildAsMinimalProxy() public {}
  function test_InitializesChildContract() public {}
  function test_SetsChildOwnerAddress() public {}
  function test_EmitsChildDeployedEvent() public {}
  function test_ReturnsAddressOfTheNewChildContract() public {}
}

contract ComputeAddress is MyFactoryTest {
  function test_ComputesExpectedAddress() public {}
}
@zerosnacks zerosnacks added good first issue Good for newcomers T-enhancement Type: enhance existing docs labels Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers T-enhancement Type: enhance existing docs
Projects
Status: Todo
Development

No branches or pull requests

2 participants