Question

I'm using ExUnit for testing my Elixir app, which is a card game.

I find that with every test I write, I start by creating a fresh 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 factor this out so that a new deck can just be created before every test case? I know there's something close to this with setup do [...] end, but I don't think that's the solution for me.

Do I need a different test framework? Do I need to use setup in some way I haven't thought of yet?

-Augie

Was it helpful?

Solution

You can use def setup with the meta has just for this.

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

Here's some more info

OTHER TIPS

Another option that could work depending on your needs:

defmodule DeckTest do
  use ExUnit.Case

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

  test "the truth" do
    assert cards == [:ace, :king, :queen]
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top