RoboZoom

RoboZoom

Configuring an Ecto App for Testing

I am trying to configure a Phoenix Ecto app for testing using the example from the docs.

Problem: Each time I run mix test, even when localized to a test that does not require DB access, I receive an error:

16:47:43.083 [error] Postgrex.Protocol (#PID<0.312.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
** (Mix) The database for MyApp.Repo couldn't be created: connection not available and request was dropped from queue after 2000ms. This means requests are coming in and your connection pool cannot serve them fast enough. You can address this by:

Following this error, it appears as if no unit tests are run.

What I’ve done: In my config/test.exs file, I have the following:

config :my_app, MyApp.Repo,
  username: "postgres",
  password: "password",
  hostname: "localhost",
  database: "daxta_server_test#{System.get_env("MIX_TEST_PARTITION")}",
  pool: Ecto.Adapters.SQL.Sandbox,
  pool_size: 10

In my mix.exs file I have:

use Mix.Project

  def project do
    [
      app: :daxta_server,
      version: "0.1.0",
      elixir: "~> 1.14",
      elixirc_paths: elixirc_paths(Mix.env()),
      start_permanent: Mix.env() == :prod,
      aliases: aliases(),
      deps: deps()
    ]
  end
  def application do
    [
      mod: {MyApp.Application, []},
      extra_applications: [:logger, :runtime_tools]
    ]
  end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

Within test/support I have a file repo_case.ex where I checkout the SQL sandbox, but for the test I’m running I don’t call this, as I am not testing anything Ecto related.

I received a suggestion to add the --no_start tag to the mix test command, but that sounds hacky. I’m sure that there’s a better way to set this up, and I’m just missing some fundamental configuration setting.

Any help is appreciated!

Most Liked

al2o3cr

al2o3cr

The Ecto sandbox doesn’t mock the database, it wraps each test in a transaction so that they don’t interfere with each other. You still need a database underneath.

Last Post!

jarlah

jarlah

that’s also why I suggest using testcontainers when making Phoenix apps. It’s less of a chore to add a couple of lines at the top of the start function in application.ex, than playing with scripts or manual docker commands, and you don’t even need to have docker installed, because Testcontainers for Desktop now has embedded runtime. The test container also shuts down if tests are completed naturally, due to use of System.at_exit. If tests are aborted, it’s cleaned up by a reaper process in docker (started by testcontainers). Ill shut up now :wink:

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
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
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

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

We're in Beta

About us Mission Statement