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! ![]()
Marked As Solved
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)
...
2
Also Liked
catra
1
sodapopcan
haha, entirely unnecessary ![]()
1
Popular in Questions
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
can someone please explain to me how Enum.reduce works with maps
New
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
I would like to know what is the best IDE for elixir development?
New
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
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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







