catra

catra

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:

Showing Posts 1 to 5

sodapopcan

sodapopcan

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

sodapopcan

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.

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

catra OP

It worked!

I owned you a drink!

Thanks @sodapopcan

sodapopcan

sodapopcan

haha, entirely unnecessary :stuck_out_tongue:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews