montre moi le test unitaire de "if (games[gameId].isPlayer2Turn == true) { return…
- User
montre moi le test unitaire de "if (games[gameId].isPlayer2Turn == true) { return games[gameId].player2;" de la fonction "function getActivePlayer() public returns (address) { gameId = getGameId(); if (games[gameId].isPlayer2Turn == true) { return games[gameId].player2; } else { return games[gameId].player1; } }" "context ("FONCTION GetActivePlayer", () => {
before(async function() { penduelInstance = await Penduel.new(subId); await penduelInstance.createGame({ from: player1 }); await penduelInstance.joinGame({ from: player2 }); }); describe ("Vérifie la fonction getActivePlayer pour player2", () => { it("doit retourner le player actif comme player2", async () => {" - Assistant
const player1 = accounts[0]; const player2 = accounts[1];
context("FONCTION GetActivePlayer", () => {
before(async function() { penduelInstance = await Penduel.new(subId); await penduelInstance.createGame({ from: player1 }); await penduelInstance.joinGame({ from: player2 }); });
describe("Vérifie la fonction getActivePlayer pour player2", () => {
it("doit retourner le player actif comme player2", async () => { const gameId = await penduelInstance.getGameId(); const isPlayer2Turn = await penduelInstance.games[gameId].isPlayer2Turn; const player2 = await penduelInstance.games[gameId].player2; assert.equal(isPlayer2Turn, true); assert.equal(await penduelInstance.getActivePlayer(), player2); });}); });
- User
TypeError: Cannot read properties of undefined (reading 'isPlayer2Turn')
- Assistant
Sorry for the error. It seems like there is an issue with accessing the properties of
games[gameId]in your tested function.To resolve this issue, you need to make sure that the
gamesmapping is properly initialized and thatgameIdis a valid key in the mapping.Here's an updated version of the test:
const player1 = accounts[0]; const player2 = accounts[1]; context("FONCTION GetActivePlayer", () => { let penduelInstance; let gameId; before(async function() { penduelInstance = await Penduel.new(subId); await penduelInstance.createGame({ from: player1 }); await penduelInstance.joinGame({ from: player2 }); gameId = await penduelInstance.getGameId(); }); describe("Vérifie la fonction getActivePlayer pour player2", () => { it("doit retourner le player actif comme player2", async () => { const isPlayer2Turn = await penduelInstance.games(gameId).isPlayer2Turn(); const player2 = await penduelInstance.games(gameId).player2(); assert.equal(isPlayer2Turn, true); assert.equal(await penduelInstance.getActivePlayer(), player2); }); }); }); - User
"isPlayer2Turn();" n'est pas une fonction mais fait parti de la structure Games
- Assistant
Je m'excuse pour la confusion. Vous avez raison,
isPlayer2Turnest un membre de la structureGameset n'est pas une fonction.Voici le code mis à jour pour accéder aux membres de la structure
Games:const player1 = accounts[0]; const player2 = accounts[1]; context("FONCTION GetActivePlayer", () => { let penduelInstance; let gameId; before(async function() { penduelInstance = await Penduel.new(subId); await penduelInstance.createGame({ from: player1 }); await penduelInstance.joinGame({ from: player2 }); gameId = await penduelInstance.getGameId(); }); describe("Vérifie la fonction getActivePlayer pour player2", () => { it("doit retourner le player actif comme player2", async () => { const games = await penduelInstance.games(gameId); const isPlayer2Turn = games.isPlayer2Turn; const player2 = games.player2; assert.equal(isPlayer2Turn, true); assert.equal(await penduelInstance.getActivePlayer(), player2); }); }); });Assurez-vous également que la structure
Gamesest correctement initialisée dans votre contratPenduelet quegameIdest une clé valide. - User
No events were emitted
0 passing (1s) 1 failing
- Contract: Penduel FONCTION GetActivePlayer Vérifie la fonction getActivePlayer pour player2 doit retourner le player actif comme player2: AssertionError: expected { Object (tx, receipt, ...) } to equal '0xd76FCcA28CE73041E079b16736c0A24968658544'
- Assistant
It appears that there is an assertion failure in your test. The expected value
0xd76FCcA28CE73041E079b16736c0A24968658544does not match the actual value returned from the function.To troubleshoot this issue, you need to inspect the values of
isPlayer2Turnandplayer2to ensure they have the expected values.Here's an updated version of the test with additional logging to help debug the issue:
const player1 = accounts[0]; const player2 = accounts[1]; context("FONCTION GetActivePlayer", () => { let penduelInstance; let gameId; before(async function() { penduelInstance = await Penduel.new(subId); await penduelInstance.createGame({ from: player1 }); await penduelInstance.joinGame({ from: player2 }); gameId = await penduelInstance.getGameId(); }); describe("Vérifie la fonction getActivePlayer pour player2", () => { it("doit retourner le player actif comme player2", async () => { const games = await penduelInstance.games(gameId); const isPlayer2Turn = games.isPlayer2Turn; const player2 = games.player2; console.log("isPlayer2Turn:", isPlayer2Turn); console.log("player2:", player2); console.log("getActivePlayer result:", await penduelInstance.getActivePlayer()); assert.equal(isPlayer2Turn, true); assert.equal(await penduelInstance.getActivePlayer(), player2); }); }); }); - User
ONCTION GetActivePlayer Vérifie la fonction getActivePlayer pour player2 isPlayer2Turn: true player2: 0xd76FCcA28CE73041E079b16736c0A24968658544 getActivePlayer result: { tx: '0xf07e1ce38809554d5693ed6a0c6f3761b75a71541aca70a3e9a5af1fe2df3832', receipt: { transactionHash: '0xf07e1ce38809554d5693ed6a0c6f3761b75a71541aca70a3e9a5af1fe2df3832', transactionIndex: 0, blockNumber: 59, blockHash: '0x2e44a6404991f47dc51532aefe6781775147886f44c39fb41648d21a0c0fb23a', from: '0xfa4c3e58301b8aa186685a72181dd337f319d81f', to: '0x28c93afcfefdc57af5d47d9bde6c38046fed267e', cumulativeGasUsed: 28102, gasUsed: 28102, contractAddress: null, logs: [], logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', status: true, effectiveGasPrice: 2500765591, type: '0x2', rawLogs: [] }, logs: [] } 1) doit retourner le player actif comme player2 > No events were emitted
- Assistant
Based on the logged values, it seems that the
getActivePlayerfunction is returning an object that includes transaction-related information (tx,receipt, etc.) instead of just the active player's address.To fix the assertion failure, you should update the assertion to compare the
player2value with thegetActivePlayerresult's address:assert.equal(player2, await penduelInstance.getActivePlayer.call(), "The active player should be player2");Make sure to use the
call()function when invokinggetActivePlayerto read the value from the contract without creating a transaction.Additionally, if you want to check for emitted events in your test, you can use the
expectEventorexpectRevertassertions from the OpenZeppelin Test Helpers library. These assertions allow you to verify whether a specific event has been emitted or whether a certain type of exception has been thrown.I hope this helps! Let me know if you have any further questions.
- User
merci !
- Assistant
De rien ! Je suis ravi de pouvoir vous aider. N'hésitez pas à poser d'autres questions si vous en avez besoin. Bonne continuation avec votre développement !
- User
peux tu faire la meme test unitaire pour le player1 ? " "function getActivePlayer() public returns (address) { gameId = getGameId(); if (games[gameId].isPlayer2Turn == true) { return games[gameId].player2; } else { return games[gameId].player1; } }" it("doit retourner le player actif comme player1", async () => {
- Assistant
Bien sûr ! Voici le test unitaire pour vérifier que la fonction
getActivePlayerrenvoie le joueur actif en tant queplayer1:describe("Vérifie la fonction getActivePlayer pour player1", () => { it("doit retourner le player actif comme player1", async () => { const games = await penduelInstance.games(gameId); games.isPlayer2Turn = false; // Rend le tour du joueur 1 const player1 = games.player1; assert.equal(await penduelInstance.getActivePlayer(), player1); }); });Assurez-vous d'inclure ce code à l'intérieur de votre bloc
contextexistant et d'ajuster les variablespenduelInstanceetgameIden fonction de votre situation réelle.Le test modifie la valeur de
isPlayer2Turndans l'objetgamespour simuler que c'est le tour du joueur 1 (player1). L'assertion vérifie ensuite si le joueur actif retourné par la fonctiongetActivePlayerest bienplayer1.N'hésitez pas à me faire savoir si vous avez d'autres questions ou besoin d'une aide supplémentaire !