Testing a Draw

The course is part of this learning path

Start course
1h 2m

In this lecture, we run through testing in Solidity and carry out a variety of tests on the game that we have been creating in previous courses in this learning path.


In the previous lecture, we were testing winning and losing by seeing only one winner. And in this lecture, I want to test how drawing works, meaning nobody wins and the game still ended. So, I'm going to add a new file called this number 3_test_draw. And we are going to change this to draw then it should be possible to end the game without a winner. So, what we're doing here now is we play it in a way that we have no winner at all, meaning our first player sets the stone 0,0. Our second player sets the stone to 1,1. Our third player sets the stone to 0,2. Our third player sets the stone to 0,3. So, we have the left top field with player one. The middle field with player two. The middle top field with player one. The top right with player two. Now our player is playing number 2,0. So, he has the left bottom. Our other player is playing 1,0. Our player is playing, where is he playing? We have the top row done, we have the left column done, we have in the center something. So, he is playing third row, second column. And the other player is playing third row, third column. And then there's only one thing left, which is third row, second row, the third column. And now let's see what this played event, how this played log looks like. I still have my ganache running. If you don't have ganache running anymore. Then run ganache now. And the second one is our powershell. And I'm going to truffle test. In the test folder we're going to test number 3 draw, and we're going to see how it looks like. I hope I didn't make any mistake. I did make a mistake somewhere. I didn't save the file. So, it's good to save the file. There is an invalid opcode. Let me see where we made the mistake. So, it seems like somebody won the game before we could draw. So, we have the first player left top, the second player in the center. The first player...Here we go. Wrong index. The first player goes to the second column. The second player goes to the third column. Game end. Here we have our transaction log here that the transaction received has a log. And inside this log we have game over withdraw. And we should also have two PayoutSuccess messages. The first one is here, PayoutSuccess, and the second one is here too because both of our players get back to 0.1 ether. So, let's just make sure that this is really working and we can remove the board. You can remove the board. You don't need the board at all. That's just, assert.equal(). We can test this by getting the balance first before they win and then the balance after they win again, and then compare that these two balances should be more, should have 0.1 ethers more. So, to get the balance, balancePlayer1BeforeDraw is web3.eth.getBalance(account[0]) and we have to await here, and player two is this one, and then after the win, AfterDraw. And then we can make sure that the balance after draw is the same as the balance from player1 before draw plus with web3.utils.toWei("0.1","ether"), "There should be a payback of 0.1 ether". Let's just make sure that this is the case here. Also for player2. Great. And the next thing is our event should be GameOverWithDraw. So, we have assert.equal(GameOverWithDraw", txReceiptPlayed.logs[2].event, "Event GameOverWithDraw should have been emitted". Let's test this again, and fingers crossed, I made no mistake. This is here. This is going to be a string, and it's just concatenating. So, we have to change this toNumber and potentially this one as well. Let me quickly have a look what balance player before draw is. I think this is a big number object. BalanceBeforeDraw, and if this is a big number object, then we should be able to convert this into a number. So, let's see what this is. It is a string. And why can't I convert this to a number? My mistake. I should call parseInt. And probably, not only here but also here at the end, parseInt which parses the string into an integer. And then you should save the file and then we rerun this, and fingers crossed it should end up with the correct number but it doesn't. Which surprises me. There should be a payback of 0.1 ethers. So, where did we make a mistake? We made the mistake by not counting the gas usage for sending off transactions. Now this doesn't work. So, we have to find another way. We can find a way by checking the PayoutSuccess, and we can check that the PayoutSuccess from the events that are emitted are going... The events that I admitted here from this last transaction receipt played are going to be the two accounts that we have in our account list. Then, let's just see that. if(txReceivedPlayed.logs[0].args.) And then let's see what the PayoutSuccess. It has a receiver here. So, if the first one is the account[0], then the second one must be the account[1] or otherwise it's reversed. So, let's just check that our txReceiptPlayed.logs[0].args_receiver, assert.equal, "Payout must be the first account") and also the amountInWei must be the same as 0.1 ether that is paid back. Must be 0.1 ether to the first account. And obviously we have also the second account here. Otherwise, we are going to have our second account as player1, and our first account as player2. Good. And then we can remove those. Let me add those back in. Comment this out, and let's say, this is not working because we have to take the gas consumption into consideration as well. Well, it is working if you take the gas consumption into consideration as well. It is not working if you just take the balance before and after and see how that works out. So, let's just give this a try test, draw. All right, and it's passing test. That is great. Now let's see how we can add as more solidity test here. Just to see how solidity tests are working with truffle. I'll see you in the next lecture.


About the Author

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.

Covered Topics