Function imported in describe block cannot be used in setup

Hi all
while refactoring some tests, I encountered a strange behavior:

I have function default_category/1 that is a fixture that creates some data in the db for testing. This function lives in its own fixtures module:

defmodule MyApp.Fixtures do

  def category_fixture(attrs \\ %{}) do
    {:ok, category} =
      attrs
      |> Enum.into(%{
        name: "Dummy Category"
      })
      |> MyApp.Categories.create_category()
  end

  def default_category(_context) do
    [default: category]
  end
end

In the tests, there are several describe blocks, one should call default_category/1 in the setup callback, the other uses it directly:

defModule MyApp.Tests do
  use MyApp.DataCase

  alias MyApp.Category
  
  describe "Will not work" do
    import MyApp.Fixtures

   setup [:default_category]

   test "First Test" do
     ...
  end

  describe "Works" do
    import MyApp.Fixtures

     test "Another test" do
       category = default_category()
       ...
    end
  end
end

Calling the default_category/1 function in the test works fine, but the setup callback does not work, I get a compiler error here, stating no default_category/1 function exisits.

The code works, if I move the import to the module level. I would prefer having the imports per describe block, so I can only import fixtures needed per block.

Do I miss something here?

Kind regards
Daniel

PS: If it matters, this is a Phoenix app.

1 Like

This may be an elixir bug. Please do a report :slight_smile:

2 Likes

Thanks @josevalim for your quick reply. I created an issue:

1 Like