Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir testing - ExUnit - Repeating Data Instances

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

like image 543
AugieDB Avatar asked Dec 25 '22 15:12

AugieDB


1 Answers

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

like image 63
Ryan Cromwell Avatar answered Jan 01 '23 15:01

Ryan Cromwell