게임 추첨! 코드 작성 및 테스트 통과

이제 우리는 테스트에 따라 모듈 게임을 코딩할 것입니다.

# lib/game.ex

defmodule Game do
  @moduledoc """
  Documentation for `Game`.
  """
end


If we run the tests now:



mix test


We'll see the tests failures:



  1) test Game.play/2 when the players draw when all players choose scissor (GameTest)
     test/game_test.exs:27
     ** (UndefinedFunctionError) function Game.play/2 is undefined or private
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) Game.play(3, 3)
       test/game_test.exs:31: (test)


  2) test Game.play/2 when the players draw when all players choose paper (GameTest)
     test/game_test.exs:18
     ** (UndefinedFunctionError) function Game.play/2 is undefined or private
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) Game.play(2, 2)
       test/game_test.exs:22: (test)


  3) test Game.play/2 when the players draw when all players choose stone (GameTest)
     test/game_test.exs:9
     ** (UndefinedFunctionError) function Game.play/2 is undefined or private
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) Game.play(1, 1)
       test/game_test.exs:13: (test)



Finished in 0.02 seconds (0.00s async, 0.02s sync)
3 tests, 3 failures


테스트를 통과하도록 합시다



먼저, 두 개의 인수인 first_player_choice와 second_player_choice를 전달하는 play 함수를 만들어야 합니다.

defmodule Game do
  @moduledoc """
  Documentation for `Game`.
  """

  def play(first_player_choice, second_player_choice) do
  end
end



그런 다음 테스트를 다시 실행해 보겠습니다.

mix test



 1) test Game.play/2 when the players draw when all players choose stone (GameTest)
     test/game_test.exs:9
     match (=) failed
     code:  assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     left:  {:ok, match}
     right: nil
     stacktrace:
       test/game_test.exs:13: (test)



  2) test Game.play/2 when the players draw when all players choose scissor (GameTest)
     test/game_test.exs:27
     match (=) failed
     code:  assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     left:  {:ok, match}
     right: nil
     stacktrace:
       test/game_test.exs:31: (test)



  3) test Game.play/2 when the players draw when all players choose paper (GameTest)
     test/game_test.exs:18
     match (=) failed
     code:  assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     left:  {:ok, match}
     right: nil
     stacktrace:
       test/game_test.exs:22: (test)



Finished in 0.03 seconds (0.00s async, 0.03s sync)
3 tests, 3 failures


이제 플레이어가 동일한 항목을 선택하면 게임이 그려지도록 보장해야 합니다.

defmodule Game do
  @moduledoc """
  Documentation for `Game`.
  """

  def play(first_player_choice, second_player_choice) do
    result(first_player_choice, second_player_choice)
  end

  defp result(first_player_choice, second_player_choice) do
    cond do
      first_player_choice == second_player_choice -> {:ok, "Draw!"}
    end
  end
end



이제 테스트를 다시 실행하면 다음과 같습니다.

mix test


모든 것이 지나갔고 모든 것이 괜찮았습니다.

...

Finished in 0.02 seconds (0.00s async, 0.02s sync)
3 tests, 0 failures

Randomized with seed 352470


다음 포스트에서는 첫 번째 플레이어가 이길 때 시나리오를 다루는 테스트를 빌드할 것입니다.

콘택트 렌즈



이메일: [email protected]
링크드인:
트위터:

좋은 웹페이지 즐겨찾기