moonlunatik

moonlunatik

Compilation error when re-ordering `use Bamboo.Test`

I have a test file that tests some of the emails we send in our app. We had the following use lines:

use MyApp.DataCase, async: true
use Bamboo.Test

We then decided to introduce a credo rule where we order our uses and imports alphabetically. The test was working fine, but if we change the use order we get this error:

== Compilation error in file lib/my_app/event_test.exs ==
** (CompileError) lib/my_app/event_test.exs:2: undefined function setup/2 (there is no such import)
    (bamboo 2.2.0) expanding macro: Bamboo.Test.__using__/1
    lib/my_app/event_test.exs:2: MyApp.EventTest (module)
    (elixir 1.14.0) expanding macro: Kernel.use/1
    lib/my_app/event_test.exs:2: MyApp.EventTest (module)

I imagine that the problem might be something that I’m defining on my DataCase module so here are the contents of the file:

defmodule MyApp.DataCase do
  use ExUnit.CaseTemplate

  alias Ecto.Adapters.SQL.Sandbox
  alias Ecto.Changeset
  alias Ecto.Query
  alias MyApp.DataCase
  alias MyApp.Repo
  alias MyApp.Tracer

  require MyApp.Tracer

  using do
    quote do
      use MyApp.Tracer.TestTracingDecorator

      alias MyApp.Repo

      import Changeset
      import DataCase
      import Ecto
      import Hammox
      import Query

      @moduletag test_case_type: :datacase_test_case
      @decorate_all trace_test(service: :my_app, tags: [test_suite: __MODULE__, test_type: :data_case])
      setup :verify_on_exit!
    end
  end

  setup context do
    Tracer.strict_span "sandbox_checkout" do
      :ok = Sandbox.checkout(Repo)

      unless context[:async] do
        Sandbox.mode(Repo, {:shared, self()})
      end
    end

    # default stubs go here
    Tracer.strict_span "stubbing" do
      # stub datetime so we don't have to mock it everywhere
      Hammox.stub_with(DateTimeMock, MyApp.Utils.DateTime)
      # stub async so we don't have to mock it everywhere
      Hammox.stub(AsyncMock, :cast, fn _function, _opts -> :ok end)

      # this allows us to dynamically stub BankAccountStorageMock
      # by tagging the test with @moduledoc :mock_bank_account_storage
      unless context[:mock_bank_account_storage] do
        # stub BankAccountStorageMock so we don't have to mock it everywhere
        # this mock is only used for cache testing/verification
        Hammox.stub_with(BankAccountStorageMock, MyApp.BankAccount.Storage)
      end

      unless context[:mock_earn_storage] do
        Hammox.stub_with(EarnStorageMock, MyApp.Earn.Storage)
      end

      unless context[:mock_user_storage] do
        Hammox.stub_with(UserStorageMock, MyApp.User.Storage)
      end
    end

    :ok
  end

  @doc """
  A helper that transforms changeset errors into a map of messages.

      assert {:error, changeset} = Accounts.create_user(%{password: "short"})
      assert "password is too short" in errors_on(changeset).password
      assert %{password: ["password is too short"]} = errors_on(changeset)

  """
  def errors_on(changeset) do
    Changeset.traverse_errors(changeset, fn {message, opts} ->
      Regex.replace(~r"%{(\w+)}", message, fn _error_msg, key ->
        opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
      end)
    end)
  end
end

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This is a mistake for use, because order does matter. use injects code, and the sequence of the code injection is semantically significant. I would disable that credo rule and have it only apply to aliases and imports.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement