8
votes
How to spoof accounts using Foundry?
There is even a simpler solution than suggested by adijo.
address someUser = vm.addr(somePrivateKey);
To get the private key either use an integer > 0, or do it more properly like this
string ...
8
votes
change transaction msg.value and msg.sender on hardhat test
It seems like the value you entered is a bit too small. I think this is why the transaction value is 0.
await contract.placeBet(game.id, 2, { value: ethers.utils.parseEther("0.5") }); // msg....
7
votes
Foundry: skip specifics tests
In addition to the @Olivier Demeaux answer.
If I needed to skip some tests I would do
Make tests internal/private. Forge will run only public or external tests
Remove the "test" word to ...
7
votes
Accepted
Foundry pranking different in test contract vs contract
Your understanding is correct. The prank cheatcode only applies to calls made from your tests; you cannot use the prank cheatcodes to change the msg.sender of the test functions themselves.
To quote ...
5
votes
Run a single hardhat test file
$ yarn hardhat test ./test/ContratA.test.js
Should work as the command line helper message explains:
$ yarn hardhat test --help
Usage: hardhat [GLOBAL OPTIONS] test [--bail] [--deploy-fixture] [-...
4
votes
Accepted
4
votes
Accepted
How to inject a fake value while testing a contract?
The short answer is you can't so have to design contracts with a testing plan in mind.
In my opinion, the root of the problem is the hard-coded oracle address. This could be abstract, allowing oracle ...
4
votes
Accepted
Is there a way to reset ETH balance in hardhat test cases?
Yes, you can use the hardhat method hardhat_setBalance during tests.
Alternatively you can revert all state changes by taking / reverting back to snapshots.
4
votes
Foundry: skip specifics tests
Yes you can!
You just have to add "no_match_test" to your foundry.toml file, then the name of the test.
I ran my tests, then added the line
no_match_test = "testClaimTreasure"
to ...
4
votes
Foundry: skip specifics tests
For any forge test, we support filtering via matches.
➜ ~ forge t --help
Run the project's tests.
Usage: forge test [OPTIONS]
Options:
-h, --help
Print help information (use `-h` for a ...
3
votes
How to spoof accounts using Foundry?
We can do this using Cheatcodes using the addr function:
pragma solidity ^0.8.6;
import "ds-test/test.sol";
interface CheatCodes {
// Gets address for a given private key, (privateKey) =...
3
votes
How to debug a solidity test
Some possible options to test things:
Use Hardhat console.log.
Use Tenderly Explorer (you can use testnet verified contracts or even local contracts using their CLI) or if the contract is on testnet,...
3
votes
Accepted
How to test abstract contracts using truffle?
No, it is not possible to test them since you can't deploy abstract contracts.
Therefore, you need to write a contract that inherits it, using the keyword "is" and implements all abstract ...
3
votes
Accessing data of struct in mapping externally
There are 2 issues:
you try calling the getters of the transactions mapping using a wrong syntax: args are between parenthesis, not brackets (ie change multisig.transactions[id] for multisig....
2
votes
Accepted
Verify a signature generated with web3 in solidity
Have a look at this: Sign message with web3 and verify with openzeppelin-solidity ECDSA.sol
Notice this little switcheroo:
signature = signature.substr(0, 130) + (signature.substr(130) == "00&...
2
votes
Accepted
How to run hardhat tests on a testnet?
Make sure your accounts have balances. And you can config the timeout depends on each network by adding a timeout property in milliseconds
(https://hardhat.org/config/#json-rpc-based-networks)
example:...
2
votes
Accepted
How can I mock ERC20 in build-in truffle test suite?
Take a look at the OpenZeppelin presets, follow this link for the ERC-20 ones.
EDIT : Giving you an example on how to use it (assuming Openzeppelin v4) :
First import the preset file, ex :
pragma ...
2
votes
Hardhat stuck testing in mainnet by default
For anyone looking for the answer, actually for some reason even tho it IS indeed the hardhat local that is running the test in the background, .getDefaultProvider().getNetwork() shows "homestead&...
2
votes
Accepted
how to subctract in type script big numbers?
The ethers.utils.parseEther function returns a BigNumber.
To make arithmetic operations on it you must explicitely call the operation implementation, in your case that would be the .sub method which ...
2
votes
How to unit-test a contract using chainlink VRF v2?
Here's an article I wrote on this which might help:
https://dev.to/abhikbanerjee99/testing-your-chainlink-vrf-powered-smart-contract-m3i
Chainlink provides mocks for data feeds aggregator and VRF (v1 ...
2
votes
Accepted
Convert big number to number on hardhat tests
You're getting an overflow error, because javascript can't represent this number with enough accuracy. Even if you were able to convert it to a number, comparing it with any other would not be ...
2
votes
How to spoof accounts using Foundry?
Sure, here's an example. First, run forge init, this will instantiate a new foundry project with the latest project structure using forge-std, Foundry's standard library which contains a bunch of ...
2
votes
How to write unit test in hardhat?
When you create a new hardhat project there is a really good example test.
The docs also explain it step by step
https://hardhat.org/tutorial/testing-contracts
You can run the test for the default ...
2
votes
Accepted
Using modifiers in Foundry `test`
It is safe to use modifiers in tests, but I cannot speak to whether it is good practice or not. It definitely does not seem to be common practice.
The main reason we have not used modifiers in example ...
2
votes
hardhat: want to test an emit event that is in a library
When you have a contract that emits an event in an underlying library instead of the contract itself, since the contract doesn't contain the ABI of the event you probably face this error:
...
2
votes
Accepted
Property 'revertedWithCustomError' does not exist on type 'Assertion'
Ok I resolved this. The documentation in both their website and in the post I reference are off.
I created a new typescript project using npx hardhat and realized they import ethers through hardhat-...
2
votes
Why do i always get this error whenever i use forge test?
can it be that you have other 'forge' cli installed?, my help looks different:
javiermac@Donosos-MacBook-Pro spool-foundry % forge help
Build, test, fuzz, debug and deploy Solidity contracts.
Usage: ...
2
votes
How to assign and send ether value together with deployment of contract using Foundry
As long as your constructor is marked payable, you can modify your test like this:
function setUp() public {
test = new testContract{value: 777}(
Owner;
);
}
You can ...
2
votes
What's a good test network right now?
Rinkeby testnet is deprecated. The only available testnet for Ethereum as of now, is Ethereum Sepolia, having ETH Chain Id value of 11155111.
In MetaMask, you can switch to Sepolia testnet by toggling ...
2
votes
Accepted
Attempting to write to Sepolia using Forge fails
You're not seeing any changes in your live contract on Sepolia Etherscan because running forge test doesn’t change values on a live deployed contract, even with --rpc-url set to a live network like ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
test × 113solidity × 57
hardhat × 43
truffle × 17
testing × 16
foundry × 16
forge × 13
unittesting × 9
chai × 9
truffle-test × 8
testnets × 7
mocha × 6
contract-development × 5
ether × 5
gas × 5
javascript × 5
ethers.js × 5
erc-20 × 4
brownie × 4
typescript × 4
tokens × 3
contract-debugging × 3
nft × 3
error × 3
gas-estimate × 3