0

I have a struct and getter function that should return the id to zero when I delete it from my function but I dont know on how to return from my testing script

Here is my struct, mapping & functions that i wanted to test From SC

struct Campaign {
        address owner;
        string title;
        string description;
        uint256 target;
        uint256 deadline;
        uint256 amountCollected;
        string image;
        address[] donators;
        uint256[] donations;
        bool claimed;
    }
// a getter mapping
 mapping(uint256 => Campaign) public campaigns;

// delete the campaign id using this function
function cancelCampaign(uint256 _id, address _owner) external {
        Campaign memory campaign = campaigns[_id];
        if (_owner != msg.sender) revert CrowdFund__NotOwner();
        if (campaign.deadline > block.timestamp) revert CrowdFund__Started();

        delete campaigns[_id];

        emit CancelCampaign(_id);
    }

// another getter function based on the total campaign created from a specific user
function getCampaigns() public view returns (Campaign[] memory) {
        Campaign[] memory allCampaigns = new Campaign[](numberOfCampaigns);

        for (uint i = 0; i < numberOfCampaigns; i++) {
            Campaign storage item = campaigns[i];

            allCampaigns[i] = item;
        }
        return allCampaigns;
    }

So on my test script, i wanted to test the cancelCampaign function and delete the id. I tested it on Remix but it returns to a struct object so I'm not sure on how to put it on my test.

Test script

describe("Cancel Campaign", () => {
    describe("Success", () => {
      let deadline: any, remove: any, result: any;
      const hundredDays = 100 * 24 * 60 * 60;
      const increasetime = async () => {
        await network.provider.send("evm_increaseTime", [hundredDays]);
        await network.provider.send("evm_mine");
      };
      beforeEach(async () => {
        await increasetime();
        deadline = await time.latest();
        await cf
          .connect(user1)
          .createCampaign(
            user1.address,
            "Test Title",
            "Test Description",
            token(1),
            deadline,
            "Image1.jpeg"
          );
        await cf.connect(user1).cancelCampaign(1, user1.address);
      });

      it("should delete the campaign id", async () => {
        expect(await cf.connect(user1).getCampaigns()).to.equal(0);
      });
    });
  });

This is what I get when I tested it in Remix:

{
    "0": "address: owner 0x0000000000000000000000000000000000000000",
    "1": "string: title ",
    "2": "string: description ",
    "3": "uint256: target 0",
    "4": "uint256: deadline 0",
    "5": "uint256: amountCollected 0",
    "6": "string: image "
}

Thanks

1 Answer 1

0

Can you asset equal to the following struct:

{
    "0": "address: owner 0x0000000000000000000000000000000000000000",
    "1": "string: title ",
    "2": "string: description ",
    "3": "uint256: target 0",
    "4": "uint256: deadline 0",
    "5": "uint256: amountCollected 0",
    "6": "string: image "
}

If thats what you are looking for anyway?

2
  • Tried to put this on my test but still getting the same result
    – 0xhaz
    Commented Dec 16, 2022 at 0:07
  • Can you check each struct position individually? Maybe make a loop function to determine the size of the mapping and that it is 0 or 1. I do wish there was more documentation for testing things like this, or if there was print functionality. In a similar situation I have amended my smart contract with check functions to be able to test certain things like this. Commented Dec 16, 2022 at 21:32

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.