Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Building the tests when players draw
Diego Novais
Diego Novais

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
Enter fullscreen modeExit fullscreen mode
  • 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
Enter fullscreen modeExit fullscreen mode
  • 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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Software Engineer | Ruby | Ruby On Rails | Elixir | Phoenix | Technical Writer | LLM
  • Location
    Brazil
  • Joined

More fromDiego Novais

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp