peux tu me montrer le test unitaire de cette fonction qui permet au gagnant de retirer…
- User
peux tu me montrer le test unitaire de cette fonction qui permet au gagnant de retirer avec succès "function withdraw(uint256 _gameId, uint256 _amount) public payable { address winner = games[_gameId].winner; currentPlayer = msg.sender; require(currentPlayer == games[_gameId].player1 || currentPlayer == games[_gameId].player2, "You re not a player"); require(!games[_gameId].player1HasWithdrawn || !games[_gameId].player2HasWithdrawn, "withdrawn balance"); uint256 balance = playerBalances[_gameId][currentPlayer].balance; require(balance >= _amount, "Insufficient balance"); require(winner == currentPlayer, "You are not the winner");
playerBalances[_gameId][currentPlayer].balance -= _amount; (bool success, ) = payable(currentPlayer).call{value: _amount}(""); require(success, "The withdrawal failed"); if (currentPlayer == games[_gameId].player1) { games[_gameId].player1HasWithdrawn = true; } else { games[_gameId].player2HasWithdrawn = true; } emit Withdraw(_gameId); }é - Assistant
Bien sûr ! Voici le test unitaire pour cette fonction :
contract TestWithdraw { // Déclarer la contract à tester YourContract contractToTest; constructor() { // Instancier le contrat à tester contractToTest = new YourContract(); } function testWithdraw() public { // Étape 1 : Déployer le contrat et initialiser les variables nécessaires // ... // Étape 2 : Appeler la fonction withdraw avec les paramètres appropriés contractToTest.withdraw(gameId, amount); // Étape 3 : Vérifier les résultats attendus // Vérifier que le solde du joueur a été mis à jour correctement uint256 expectedBalance = initialBalance - amount; uint256 actualBalance = contractToTest.playerBalances[gameId][currentPlayer].balance; Assert.equal(actualBalance, expectedBalance, "The player balance should be updated correctly"); // Vérifier que le retrait a réussi bool expectedSuccess = true; bool actualSuccess = contractToTest.success; Assert.equal(actualSuccess, expectedSuccess, "The withdrawal should be successful"); // Vérifier que le joueur a été marqué comme ayant effectué un retrait dans le jeu approprié bool expectedWithdrawn = true; bool actualWithdrawn; if (currentPlayer == contractToTest.games[gameId].player1) { actualWithdrawn = contractToTest.games[gameId].player1HasWithdrawn; } else { actualWithdrawn = contractToTest.games[gameId].player2HasWithdrawn; } Assert.equal(actualWithdrawn, expectedWithdrawn, "The player should be marked as withdrawn in the correct game"); // Vérifier que l'événement Withdraw a été émis bool expectedEventEmitted = true; bool actualEventEmitted = contractToTest.withdrawn; Assert.equal(actualEventEmitted, expectedEventEmitted, "The Withdraw event should be emitted"); } }