Is there any way to test with multiple addresses using forge like you can in Truffle or Hardhat? Like Truffle gives you an array of addresses you can call and that changes the msg.sender
calling the function.
I'm trying to test these methods:
function addMember(address _member) public onlyOwner {
balance[_member] = TOTAL_MEMBER_TOKENS;
totalTokens += TOTAL_MEMBER_TOKENS;
}
function addProposal(string calldata _proposal) public onlyMember {
proposals.push(Proposal(_proposal, 0, 0));
emit NewProposal(_proposal);
}
I want to use the owner address to addMember
then use the members address to call addProposal
. How would I do that? If this can't be done then what is the address that I should pass to addMember
to make the owner of the contract a member so I can test with only one address.
Best Answer
Foundry's
anvil
network provides you with a list of 10 addresses and their private keys:You can easily use these to test your functions.
Another solution you could use would be to simply use
vm.prank
with random addresses, such asaddress(0)
,address(1)
, andaddress(1337)
.First you can test
addMember()
by calling it from the owner's address with a random address such asaddress(1)
.For example:
vm.prank
allows you to changemsg.sender
for the next function call.Now that
address(1)
is a member we can calladdProposal()
from that address:You can add some conditional or
assert
after the function call to ensure it did what you expected it to.You can read more about Foundry's
anvil
here and more aboutvm.prank
here.