I'm doing Unit testing and I need to set/add a mapping to my smart contract for some bypassing for a certain test case.
I have a mapping in my smartcontract like so:
mapping(string => uint256) public claimTransactions;
I have no problem reading the smart contract's mapping:
await contract.claimTransactions("2");
returns a BigInt of the integer mapping of the passed string
However, when I need to set a value or a mapping in my unit test file using ethers.js:
I'm trying to mimic this line of code from my smartcontract (solidity)
claimTransactions[txId] = amount;
I need help if this is possible, or any reference that I can check on would be very helpful.
Best Answer
When you defined the
claimTransactions
mapping, solidity automatically created agetter
method for it, which you accessed by doingclaimTransactions(string)
. However, solidity does not automatically create asetter
method for it, which is required to change the state.You would have to define your own setter method and then call it in your javascript
then in your javascript: