Skip to main content
deleted 1 character in body
Source Link
 function testMakeOffer() public {
        Offer[] memory offers;
        eProp.mintProp(USER, 44, 65, 1, 1, 0);

        vm.prank(USER2);
        eProp.makeOffer(0, 1 ether);
 
        vm.prank(USER);
        eProp.Offer[] memory offers = eProp.getOffers(0);

        assert(offers[0].sender == USER2);
        assert(offers[0].offerdAmount == 1 ether);
    }
 function testMakeOffer() public {
        Offer[] memory offers;
        eProp.mintProp(USER, 44, 65, 1, 1, 0);

        vm.prank(USER2);
        eProp.makeOffer(0, 1 ether);
 
        vm.prank(USER);
        offers = eProp.getOffers(0);

        assert(offers[0].sender == USER2);
        assert(offers[0].offerdAmount == 1 ether);
    }
 function testMakeOffer() public {
        
        eProp.mintProp(USER, 44, 65, 1, 1, 0);

        vm.prank(USER2);
        eProp.makeOffer(0, 1 ether);
        
        eProp.Offer[] memory offers = eProp.getOffers(0);

        assert(offers[0].sender == USER2);
        assert(offers[0].offerdAmount == 1 ether);
    }
Source Link

Problem with testing mappings and structs in foundry

Hey everyone

I'm using foundry for solidity development and I have some issues with using mappings and structs in my tests.

here are my problems:

Problem 1

the problem :

the function below lets users list their tokens for sale but when I want to test them, I get this error on assert(eProp.tokenIdToIsListed[0]) :

Indexed expression has to be a type, mapping or array (is function (uint256) view external returns (bool))

the function :

mapping(uint256 => bool) public tokenIdToIsListed;
mapping(uint256 => uint256) public tokenIdToPrice;

function listTokenForSale(
        uint256 tokenId,
        uint256 price
    ) public onlyTokenOwner(tokenId) {
        if (!_exists(tokenId)) {
            revert wrongTokenIdEnterd();
        }
        tokenIdToIsListed[tokenId] = true;
        tokenIdToPrice[tokenId] = price;
    }

the test :

function testListTokenForSale() public {
        eProp.mintProp(USER, 44, 65, 1, 1, 0);
        uint256 sellPrice = 1 ether;

        vm.prank(USER);
        eProp.listTokenForSale(0, sellPrice);

        vm.prank(USER);
        vm.expectRevert();
        eProp.listTokenForSale(2, sellPrice);

        assert(eProp.tokenIdToIsListed[0]);
        assert(eProp.tokenIdToPrice[0] == sellPrice);
    }

Problem 2

the problem :

the function below lets users make an offer and saves and pushes the offer in an array of offers in a private mapping. the other function lets the owner of tokens get the offers for their token.in this case, for testing these functions I need to get the offer object in my test.

I tried the library solution and made a library having my offer struct in it and then imported the library in my test file and it worked but in this case, I have to use the library for my whole contract and it feels not okay to me.

isn't there any other way to test my structs in Foundry?

the functions :

mapping(uint256 => Offer[]) private tokenIdToOffers;

function makeOffer(uint256 tokenId, uint256 offeredPrice) public {
        if (!_exists(tokenId) || ownerOf(tokenId) == msg.sender) {
            revert wrongTokenIdEnterd();
        }
        if (tokenIdToHighestBid[tokenId] != 0 || tokenIdToIsListed[tokenId]) {
            revert tokenAlreadyInSaleOrAuction();
        }
        Offer memory offer = Offer(msg.sender, offeredPrice);
        tokenIdToOffers[tokenId].push(offer);
    }

function getOffers(
        uint256 tokenId
    ) external view onlyTokenOwner(tokenId) returns (Offer[] memory) {
        return tokenIdToOffers[tokenId];
    }

the test :

 function testMakeOffer() public {
        Offer[] memory offers;
        eProp.mintProp(USER, 44, 65, 1, 1, 0);

        vm.prank(USER2);
        eProp.makeOffer(0, 1 ether);

        vm.prank(USER);
        offers = eProp.getOffers(0);

        assert(offers[0].sender == USER2);
        assert(offers[0].offerdAmount == 1 ether);
    }