grangerelixx
Seed file not loading factory dependency
Any suggestions on how to load dependency of factory in seeds file?
factory.ex
alias Test/Utils/TextGenerator
def post_factory(attrs \\ %{}) do
%Post{
body: TextGenerator.text_fake()
}
end
seeds.exs
{factory, _} = Code.require_file("factory.ex", "./test/support/") |> hd()
alias Blog.Posts
{time, _} =
:timer.tc(fn ->
tasks =
Enum.map(1..100, fn _ ->
Task.async(fn ->
post =
factory.post_factory
|> Map.from_struct()
Enum.each(1..100, fn _ -> Posts.create_post(post) end)
end)
end)
Task.await_many(tasks, 20_000)
end)
This is the error I get…
** (UndefinedFunctionError) function Blog.Utils.TextGenerator.text_fake/0 is undefined (module Blog.Utils.TextGenerator is not available)
Blog.Utils.TextGenerator.text_fake()
test/support/factory.ex:23: Blog.Factory.post_factory/1
priv/repo/seeds.exs:34: anonymous fn/1 in :elixir_compiler_1.__FILE__/1
(elixir 1.11.3) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
(elixir 1.11.3) lib/task/supervised.ex:35: Task.Supervised.reply/5
(stdlib 3.14) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Function: #Function<2.133263724 in file:priv/repo/seeds.exs>
Args: []
Most Liked Responses
al2o3cr
A typical approach with using factory-ish files in seeds.exs is to include test/support when compiling in dev:
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(:dev), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
(normally only :test includes that directory)
This should avoid any need to manually invoke Code.require_file.
2
Popular in Questions
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
I have a super simple question about elixir - how would I take a file like this
foo
bar
baz
and output a new file that enumerates th...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
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
Other popular topics
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
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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
Hello, how can I check the Phoenix version ?
Thanks !
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
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









