1

Hi Iam new in solidity program i don't know how to test the donate function can anybody help me how to test? Actually I want to test when some donate the donater balance is minus and the person who created a campaign his balance is increase when someone donate.

// SPDX-License-Identifier: Unlicensed

pragma solidity >0.7.0 <=0.9.0;

contract CampaignFactory {
    address[] public deployedCampaigns;

    event campaignCreated(
        string title,
        uint requiredAmount,
        address indexed owner,
        address campaignAddress,
        string imgURI,
        uint indexed timestamp,
        string indexed category
    );

    function createCampaign(
        string memory campaignTitle, 
        uint requiredCampaignAmount, 
        string memory imgURI, 
        string memory category,
        string memory storyURI) public
    {

        Campaign newCampaign = new Campaign(
            campaignTitle, requiredCampaignAmount, imgURI, storyURI, msg.sender);
        

        deployedCampaigns.push(address(newCampaign));

        emit campaignCreated(
            campaignTitle, 
            requiredCampaignAmount, 
            msg.sender, 
            address(newCampaign),
            imgURI,
            block.timestamp,
            category
        );

    }
}


contract Campaign {
    string public title;
    uint public requiredAmount;
    string public image;
    string public story;
    address payable public owner;
    uint public receivedAmount;

    event donated(address indexed donar, uint indexed amount, uint indexed timestamp);

    constructor(
        string memory campaignTitle, 
        uint requiredCampaignAmount, 
        string memory imgURI,
        string memory storyURI,
        address campaignOwner
    ) {
        title = campaignTitle;
        requiredAmount = requiredCampaignAmount;
        image = imgURI;
        story = storyURI;
        owner = payable(campaignOwner);
    }

Here is donate function

    function donate() public payable {
        require(requiredAmount > receivedAmount, "required amount fullfilled");
        owner.transfer(msg.value);
        receivedAmount += msg.value;
        emit donated(msg.sender, msg.value, block.timestamp);
    }
}

1 Answer 1

1

You can use Foundry (recommended) or Hardhat for testing.

This is a good course by @patrick-collins which goes in depth https://www.youtube.com/watch?v=umepbfKp5rI&t=278s

Recommended to read: https://fravoll.github.io/solidity-patterns/checks_effects_interactions.html

Here's an outline of a test in Hardhat

// 1. Import necessary modules and libraries

describe("Campaign", function () {
  let campaign;
  let owner;

  // 2. Define beforeEach hook to deploy a fresh instance of the contract

  beforeEach(async function () {
    // a. Deploy the contract using the Contract Factory
    // b. Get the owner's signer
  });

  // 3. Write the test case for the donate function

  it("should update received amount and decrease contract balance when donating", async function () {
    // a. Get the initial balance of the owner
    // b. Define the donation amount

    // c. Call the donate function with the donation amount

    // d. Assert that the received amount is updated correctly
    // e. Assert that the contract balance of the owner has decreased by the donation amount
  });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.