catra

catra

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:

Marked As Solved

sodapopcan

sodapopcan

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)
...

Also Liked

catra

catra

It worked!

I owned you a drink!

Thanks @sodapopcan

sodapopcan

sodapopcan

haha, entirely unnecessary :stuck_out_tongue:

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement