Elixir Validation - ExUnit - Duplicate Data Instances

I use ExUnit to test my Elixir application, which is a card game.

I find that with every test I write, I start by creating a new deck of cards.

test "Do This Crazy Thing do
  deck = Deck.create()
  [...]
end

test "Do This Other Crazy Unrelated Thing" do
  deck = Deck.create()
   [...]
end

Is there a way to explain this so that a new deck can only be created before each test case? I know something close to this with setup do [...] end, but I don't think the solution is for me.

Do I need another test environment? Should I use it setupin some way that I haven't thought about?

-Augie

+4
source share
2 answers

You can use def setupwith metajust that.

Example:

defmodule DeckTest do
  use ExUnit.Case

  setup do
    {:ok, cards: [:ace, :king, :queen] }
  end

  test "the truth", meta do
    assert meta[:cards] == [:ace, :king, :queen]
  end
end

+9

, :

defmodule DeckTest do
  use ExUnit.Case

  defp cards, do: [:ace, :king, :queen]

  test "the truth" do
    assert cards == [:ace, :king, :queen]
  end
end
+1

All Articles