vial-virusCh 4: Testing

In this chapter, you'll write tests to verify that the ClownBeatdown contract behaves as expected under various scenarios. Testing ensures the functionality, fairness, and access control mechanisms of your contract work seamlessly, particularly in multi-round gameplay. Estimated Time: ~15 minutes.

Getting Started

Navigate to the test folder in your project and open the ClownBeatdown.t.sol file located at:

packages/contracts/test/ClownBeatdown.t.sol

This file is where you'll write all the test cases for the ClownBeatdown contract. Start with the following base code:

// SPDX-License-Identifier: MIT License
pragma solidity ^0.8.13;

import {Test} from "forge-std/Test.sol";
import {ClownBeatdown} from "../src/ClownBeatdown.sol";

contract ClownBeatdownTest is Test {
    ClownBeatdown public clownBeatdown;

    function setUp() public {
        clownBeatdown = new ClownBeatdown(2);
        clownBeatdown.addSecret("Secret A");
        clownBeatdown.addSecret("Secret B");
        clownBeatdown.addSecret("Secret C");
    }
}

The setUp() function initializes the ClownBeatdown contract with a stamina of 2 and adds three secrets to the pool.

Writing Test Cases

Start off with testing the basic functionalities: hit, rob, and reset.

Core functionalities

  1. Basic hit functionality

Ensures the clown's stamina decreases when hit.

  1. Knockout and rob

Validates that after knocking out the clown, a contributor can rob a valid secret.

  1. Reset functionality

  1. Secret can change after reset

Validates that secrets returned across rounds are always valid (they may or may not differ depending on randomness).

Now, test for the restrictive/conditional nature of these basic functionalities.

Restricting Actions

  1. Preventing hit when clown is down

Ensures that hitting a knocked-out clown is not allowed.

  1. Preventing rob when clown is standing

Ensures that robbing while the clown still has stamina is not allowed.

  1. Preventing reset when clown is standing

Validates that the clown cannot be reset unless it is fully knocked out.

Now, test for more complex scenarios.

Complex scenarios

  1. Prevent Non-Contributors From Using rob()

Ensures that only contributors in the current round can call rob().

  1. Contributor Tracking Across Rounds

Validates that contributions are tracked independently for each round. The test has one contributor knock out the clown in round 1, and a different contributor knock it out in round 2. We check that the round 2 contributor can rob while the round 1 contributor cannot.

Final Test Contract

Test out the file by running the following inside the packages/contracts directory:

The contract has been tested, time to deploy it!

Last updated