Skip to content

Section 3 Lesson 30: Test Cases

Thiago Salles edited this page Jul 2, 2021 · 1 revision

Here's an example on how to create test cases.

I like to organize function into a describe and put all its test cases inside of it:

  describe "create_deck/1" do
    test "makes 20 cards" do
      deck = Cards.create_deck()

      assert length(deck) == 20
    end
  end

  describe "deal/2" do
    test "works if deck has enough cards" do
      deck = Cards.create_deck()
      hand_size = 5

      assert {:ok, hand, rest_of_deck} = Cards.deal(deck, hand_size)
      assert length(hand) == hand_size
      assert length(rest_of_deck) == length(deck) - hand_size
    end

    test "fails if deck has not enough cards" do
      deck = Cards.create_deck()

      assert {:error, _reason} = Cards.deal(deck, 500)
    end
  end