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 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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay