ThrashAbaddon

ThrashAbaddon

How to seed database for testing?

I have a seed.exs and I want to use it before mix test to seed db and then use that data inside tests. How do I do that?

Marked As Solved

michalmuskala

michalmuskala

You can run your seeds inside the test_helper.exs file. It’s always evaluated before the tests.

Michał.

Also Liked

chrislaskey

chrislaskey

In order to call the seeds from inside test_helper.exs, one option is to move the seeds file into a separate module:

defmodule MyApp.Seeds do
  def call do
    # Seed actions
  end
end

Then MyApp.Seeds.call can easily be called from either priv/repo/seeds.exs or test/test_helper.exs.

Thanks to Ihor Katkov in the #ecto Slack channel for proposing the solution.

rmoorman

rmoorman

Another way to do it currently is to call add run path/to/test_seeds.exs to the test alias in the projects mix.exs:

  defp aliases do
    [
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      "test": ["ecto.create --quiet", "ecto.migrate", "run test/test_seeds.exs", "test"]
    ]
  end

One thing to keep in mind though is that the seeds are run (of course) every time mix test is invoked, so the seeds will probably need to have some kind of conditional logic in place to only insert items in case they do not exist yet, something like this for example:

alias Foo.Category
alias Foo.Repo

if !Repo.get_by(Category, name: "fascinating") do
  Repo.insert(%Category{name: "fascinating"})
end

Then again, it might be more appropriate to insert the needed database records within the tests itself (or case file), which might make the tests a bit more explicit and self-contained and no conditional logic would be needed because tests are run within a transaction/cleaned up automatically.

ThrashAbaddon

ThrashAbaddon

Thanks Michał.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
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
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
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
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement