Dynamically import factory in DataCase

In one of my test files, I found that on the top there is a use MyProject.DataCase statement. Inside of MyProject.DataCase there is a using macro where there are some import/alias. One of them is to import a factory module and I’m wondering if there is a way to define which factory to use depending on a key passed to use MyProject.DataCase, something like:

defmodule MyProject.SomeTest do
  use MyProject.DataCase, async: true, factory: MyProject.FactoryTwo
...

On the DataCase

defmodule MyProject.DataCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      alias MyProject.Repo
      import Keyword.get(opts, :factory, MyProject.FactoryOne) # <-- I don't know if I can get the argument passed
...

Some refs:

Thanks in advance! :smiley:

You should just be able to add an opts arg to the using do line then unquote opts and what you’re looking to do should work:

  ...
  using opts do
    quote do
      import Keyword.get(unquote(opts), :factory, MyProject.FactoryOne)
  ...

Wait, I’m sorry, you can’t use import like that. I know I’ve done something like this but it’s in a repo that I don’t have access to anymore.

This article may help.

Sorry for all the replies. This should work (I actually tested it this time, lol):

defmodule MyProject.DataCase do
  use ExUnit.CaseTemplate

  using opts do
    factory_module = Keyword.get(opts, :factory, MyProject.FactoryOne)

    quote do
      alias MyProject.Repo
      import unquote(factory_module)
...
2 Likes

It worked!

I owned you a drink!

Thanks @sodapopcan

1 Like

haha, entirely unnecessary :stuck_out_tongue:

1 Like