Facing problem while writing the Test for "mintDsc()" in Stablecoins project. #137
-
🔗💻Here is my github repo link: https://github.com/APrakash-913/FOUNDRY_StableCoins I'm getting problem in testing "mintDsc()" function.⬇️ function mintDsc(
uint256 amountDscUserWantToMint
) public moreThanZero(amountDscUserWantToMint) nonReentrant {
s_DSCMinted[msg.sender] = amountDscUserWantToMint;
_revertIfHealthFactorIsBroken(msg.sender);
bool minted = i_dsc.mint(msg.sender, amountDscUserWantToMint);
if (!minted) {
revert DSCEngine__DSCMintingFailed();
}
emit DSCMinted(msg.sender, amountDscUserWantToMint);
} And my test code is ⬇️: address public USER = makeAddr("USER");
uint256 public constant MINT_VALUE = 1 ether;
uint256 public constant DEPOSIT_VALUE = 10 ether;
uint256 public constant STARTING_BALANCE = 1000 ether;
modifier depositedCollateral() {
vm.startPrank(USER);
ERC20Mock(weth).approve(address(dscEngine), STARTING_BALANCE);
dscEngine.depositCollateral(weth, DEPOSIT_VALUE);
vm.stopPrank();
_;
}
function setUp() external {
deployer = new DeployDSCEngine();
(dscEngine, dsc, helperConfig) = deployer.run();
(ethUsdPriceFeed, btcUsdPriceFeed, weth, wbtc, ) = helperConfig
.activeNetworkConfig();
// 🏦 Providing fund to User.
ERC20Mock(weth).mint(USER, STARTING_BALANCE);
}
function testMappingIsPopulatedByMintDsc() public depositedCollateral {
(uint256 dsc1, uint256 collateral) = dscEngine.getAccountInfo(USER);
console.log(dsc1, collateral); // output: (0,0)
dscEngine.mintDsc(MINT_VALUE);
// console.log(dsc.balanceOf(USER));
// console.log(MINT_VALUE);
assertEq(MINT_VALUE, dsc.balanceOf(USER));
} error message is⬇️:
I've already done necessary changes in the _healthFactor func, but still getting error. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @APrakash-913, if I checked everything correctly... the issue is the setup when you execute Who is calling This test should pass if you start a new prank for function testMappingIsPopulatedByMintDsc() public depositedCollateral {
//start a new prank
vm.startPrank(USER);
dscEngine.mintDsc(MINT_VALUE);
vm.stopPrank();
assertEq(MINT_VALUE, dsc.balanceOf(USER));
} |
Beta Was this translation helpful? Give feedback.
Hello @APrakash-913, if I checked everything correctly... the issue is the setup when you execute
testMappingIsPopulatedByMintDsc
.Who is calling
mintDsc
in this test? With you current setup is notUSER
, it is in fact a default account from Anvil, and the test will fail because this account have no collateral in the contract, therefore it cannot mintDSC
.This test should pass if you start a new prank for
USER
to calldscMint
: