DEV Community

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

Posted on • Updated 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 choose stone:
defmodule GameTest do
  use ExUnit.Case

  @stone 1
  @paper 2
  @scissor 3

  describe "Game.play/2 when the players draw" do
    test "when all players choose stone" do
      first_player_choice = @stone
      second_player_choise = @stone

      assert {:ok, match} = Game.play(first_player_choice, second_player_choise)

      assert match == "Draw!"
    end
  end
end
Enter fullscreen mode Exit fullscreen mode
  • So, if the players choose paper:
defmodule GameTest do
  use ExUnit.Case

  @stone 1
  @paper 2
  @scissor 3

  describe "Game.play/2 when the players draw" do
    #...

    test "when all players choose paper" do
      first_player_choice = @paper
      second_player_choise = @paper

      assert {:ok, match} = Game.play(first_player_choice, second_player_choise)

      assert match == "Draw!"
    end
  end
end
Enter fullscreen mode Exit fullscreen mode
  • So, if the players choose scissor:
defmodule GameTest do
  use ExUnit.Case

  @stone 1
  @paper 2
  @scissor 3

  describe "Game.play/2 when the players draw" do
    #...
    test "when all players choose scissor" do
      first_player_choice = @scissor
      second_player_choise = @scissor

      assert {:ok, match} = Game.play(first_player_choice, second_player_choise)

      assert match == "Draw!"
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Let's look at the code of the tests when the game's result is "Draw!".

defmodule GameTest do
  use ExUnit.Case

  @stone 1
  @paper 2
  @scissor 3

  describe "Game.play/2 when the players draw" do
    test "when all players choose stone" do
      first_player_choice = @stone
      second_player_choise = @stone

      assert {:ok, match} = Game.play(first_player_choice, second_player_choise)

      assert match == "Draw!"
    end

    test "when all players choose paper" do
      first_player_choice = @paper
      second_player_choise = @paper

      assert {:ok, match} = Game.play(first_player_choice, second_player_choise)

      assert match == "Draw!"
    end

    test "when all players choose scissor" do
      first_player_choice = @scissor
      second_player_choise = @scissor

      assert {:ok, match} = Game.play(first_player_choice, second_player_choise)

      assert match == "Draw!"
    end
  end
end
Enter fullscreen mode Exit 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)