Replies: 3 comments 1 reply
-
Hello @jayantna, whenever you use function test_deployerGetsMintedInitialBalance() public{
uint256 expectedDeployerBalance = 100 ether;
uint256 actualDeployerBalance = myTokenContract.balanceOf(msg.sender);
console2.log("balance of the deploye is: ", actualDeployerBalance);
console2.log("The address of the balance is: ", msg.sender);
assertEq(actualDeployerBalance, expectedDeployerBalance);
} The below is my result of running the above test code forge test --mt test_deployerGetsMintedInitialBalance -vvvv
[⠊] Compiling...
[⠒] Compiling 1 files with Solc 0.8.23
[⠑] Solc 0.8.23 finished in 1.52s
Compiler run successful!
Ran 1 test for test/MyTokenTest.t.sol:MyTokenTest
[PASS] test_deployerGetsMintedInitialBalance() (gas: 13119)
Logs:
balance of the deployer is: 100000000000000000000
The address of the deployer is: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
Traces:
[13119] MyTokenTest::test_deployerGetsMintedInitialBalance()
├─ [2851] MyToken::balanceOf(DefaultSender: [0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38]) [staticcall]
│ └─ ← [Return] 100000000000000000000 [1e20]
├─ [0] console::log("balance of the deployer is: ", 100000000000000000000 [1e20]) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("The address of the deployer is: ", DefaultSender: [0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38]) [staticcall]
│ └─ ← [Stop]
└─ ← [Stop]
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 14.46ms (2.19ms CPU time)
Ran 1 test suite in 2.70s (14.46ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests) Please ask questions if anything is confusing. |
Beta Was this translation helpful? Give feedback.
-
@EngrPips Thanks for the help, to verfiy the owner I have made an immutable owner address constructor and tested its value in test file, I actually got contract Token is ERC20 {
address public immutable i_owner;
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
ERC20(tokenName, tokenSymbol) {
i_owner = msg.sender;
_mint(msg.sender, initialSupply);
}
} function testGetOwner() public view{
console.log("Token i_owner inside test function", token.i_owner());
} forge test --mt testGetOwner -vv
[⠊] Compiling...
[⠒] Compiling 1 files with Solc 0.8.28
[⠑] Solc 0.8.28 finished in 574.51ms
Compiler run successful!
Ran 1 test for test/TestDeployToken.t.sol:TestDeployToken
[PASS] testGetOwner() (gas: 9362)
Logs:
Token i_owner inside test function 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 6.08ms (1.43ms CPU time) While following Foundry Fundamentals course, i_owner() result is actually different, so my doubt is what is making the difference here? function testOwnerIsMsgSender() public {
console.log(fundMe.i_owner());
console.log(msg.sender);
assertEq(fundMe.i_owner(), msg.sender);
} Ran 2 tests for test/FundMe.t.sol:FundMeTest
[PASS] testMinimumDollarIsFive() (gas: 5453)
[FAIL. Reason: assertion failed] testOwnerIsMsgSender() (gas: 26680)
Logs:
0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496
0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
Error: a == b not satisfied [address]
Left: 0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496
Right: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
Suite result: FAILED. 1 passed; 1 failed; 0 skipped; finished in 975.40µs (449.20µs CPU time)
Ran 1 test suite in 301.60ms (975.40µs CPU time): 1 tests passed, 1 failed, 0 skipped (2 total tests)
Failing tests:
Encountered 1 failing test in test/FundMe.t.sol:FundMeTest
[FAIL. Reason: assertion failed] testOwnerIsMsgSender() (gas: 26680)
|
Beta Was this translation helpful? Give feedback.
-
Nevermind, I got my answer also I will summarise it here for other's reference:
|
Beta Was this translation helpful? Give feedback.
-
Test for token balance in deployer address is failing. I have tried debugging it but didn't find the root cause, maybe I am missing something so here I need community help to understand this.
Token.sol
My ERC20 Token contract uses 3 arguments in constructor:
DeployToken.s.sol
Deploy script takes those 3 args in its
run()
function parameter.TestDeployToken.t.sol
testBalanceOfOwner()
test is failing, while debugging deployer address token balance is 0.Test logs
Beta Was this translation helpful? Give feedback.
All reactions