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
You can run your seeds inside the test_helper.exs file. It’s always evaluated before the tests.
Michał.
Also Liked
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
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
Thanks Michał.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









