ExUnit Setup - Options for Idiomatic Struct Inserts

Hi all! Wondering how to best use a setup block for inserting some duplicate values for a todo_list struct. For example-

defmodule ToDoTest do
  use ExUnit.Case

describe "entries/2" do
    test "entries should successfully be fetched when called by a specific date" do
      todo_list = %TodoList2{
        auto_id: 2,
        entries: %{1 => %{date: ~D[2018-12-19], id: 1, title: "Dentist"}}
      }

      date = ~D[2018-12-19]

      assert TodoList2.entries(todo_list, date) == [
               %{date: ~D[2018-12-19], title: "Dentist", id: 1}
             ]
    end

    test "empty list should be returned when no entries exist for a specific date" do
      todo_list = %TodoList2{
        auto_id: 2,
        entries: %{1 => %{date: ~D[2018-12-19], id: 1, title: "Dentist"}}
      }

      date = ~D[2018-12-20]

      assert TodoList2.entries(todo_list, date) == []
    end
  end

end

The two tests use the same struct value for todo_list, how do I correctly use a setup after my `describe “entries/2” so I’m not repeating myself here? Haven’t quite figured this out from the ExUnit docs as they’re pretty basic. Thanks for any help :slight_smile:

it would be something like this

defmodule ToDoTest do
  use ExUnit.Case

  describe "entries/2" do
    setup do
     todo_list = %TodoList2{
        auto_id: 2,
        entries: %{1 => %{date: ~D[2018-12-19], id: 1, title: "Dentist"}}
      }

      [todo_list: todo_list]
    end

    test "entries should successfully be fetched when called by a specific date", %{todo_list: todo_list} do
      date = ~D[2018-12-19]

      assert TodoList2.entries(todo_list, date) == [
               %{date: ~D[2018-12-19], title: "Dentist", id: 1}
             ]
    end

    test "empty list should be returned when no entries exist for a specific date", %{todo_list: todo_list} do
      date = ~D[2018-12-20]

      assert TodoList2.entries(todo_list, date) == []
    end
  end
end
1 Like

Bingo! Thanks Tom :purple_heart:

1 Like