
Posted on • Edited on
Building the tests when players draw
Now we'll develop the tests, and after, we'll make the tests pass, and subsequently, we'll make some refactoring.
Building the tests
Inside the module Game, we'll create the function called play, which will receive the choice of the first player and the second player as arguments.
And with this data, we'll make some calculus, and then we'll know who the winner is.
So, let's go to tests and scenarios...
When players draw
When the first and second players choose the same item, the game is Draw!
- So, if the players choosestone:
defmoduleGameTestdouseExUnit.Case@stone1@paper2@scissor3describe"Game.play/2 when the players draw"dotest"when all players choose stone"dofirst_player_choice=@stonesecond_player_choise=@stoneassert{:ok,match}=Game.play(first_player_choice,second_player_choise)assertmatch=="Draw!"endendend
- So, if the players choosepaper:
defmoduleGameTestdouseExUnit.Case@stone1@paper2@scissor3describe"Game.play/2 when the players draw"do#...test"when all players choose paper"dofirst_player_choice=@papersecond_player_choise=@paperassert{:ok,match}=Game.play(first_player_choice,second_player_choise)assertmatch=="Draw!"endendend
- So, if the players choosescissor:
defmoduleGameTestdouseExUnit.Case@stone1@paper2@scissor3describe"Game.play/2 when the players draw"do#...test"when all players choose scissor"dofirst_player_choice=@scissorsecond_player_choise=@scissorassert{:ok,match}=Game.play(first_player_choice,second_player_choise)assertmatch=="Draw!"endendend
Let's look at the code of the tests when the game's result is
"Draw!"
.
defmoduleGameTestdouseExUnit.Case@stone1@paper2@scissor3describe"Game.play/2 when the players draw"dotest"when all players choose stone"dofirst_player_choice=@stonesecond_player_choise=@stoneassert{:ok,match}=Game.play(first_player_choice,second_player_choise)assertmatch=="Draw!"endtest"when all players choose paper"dofirst_player_choice=@papersecond_player_choise=@paperassert{:ok,match}=Game.play(first_player_choice,second_player_choise)assertmatch=="Draw!"endtest"when all players choose scissor"dofirst_player_choice=@scissorsecond_player_choise=@scissorassert{:ok,match}=Game.play(first_player_choice,second_player_choise)assertmatch=="Draw!"endendend
In the next post, we'll code our module Game following the tests.
Contacts
Email:contato@diegonovais.com.br
Linkedin:https://www.linkedin.com/in/diegonovais/
Twitter:https://twitter.com/diegonovaistech
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse