Reputation: 8988
I have a method called insert_user which is working fine in other parts of the application but for some reason it's undefined in this case and cannot figure out why
** (CompileError) test/models/user_repo_test.exs:8: undefined function insert_user/1
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:376: Code.require_file/2
(elixir) lib/kernel/parallel_require.ex:59: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5
This is the code:
defmodule Rumbl.UserRepoTest do
use Rumbl.ModelCase
alias Rumbl.User
@valid_attrs %{name: "A User", username: "eva"}
test "converts unique_constraint on username to error" do
insert_user(username: "eric")
attrs = Map.put(@valid_attrs, :username, "eric")
changeset = User.changeset(%User{}, attrs)
assert {:error, changeset} = Repo.insert(changeset)
assert {:username, "has already been taken"} in changeset.errors
end
end
This is the definition at test/support/test_helpers.ex
defmodule Rumbl.TestHelpers do
alias Rumbl.Repo
def insert_user(attrs \\ %{}) do
changes = Enum.into(attrs, %{
name: "Some User",
username: "user#{Base.encode16(:crypto.strong_rand_bytes(8))}",
password: "supersecret",
})
%Rumbl.User{}
|> Rumbl.User.registration_changeset(changes)
|> Repo.insert!()
end
def insert_video(user, attrs \\ %{}) do
user
|> Ecto.build_assoc(:videos, attrs)
|> Repo.insert!()
end
Upvotes: 1
Views: 60
Reputation: 5812
If you didn't import Rumbl.TestHelpers
in the Rumbl.ModelCase
in using
macro block, you have to add it explicitly in your test suite to have this functions imported.
It should help, but otherwise - please check that your TestHelpers
is placed in test/support
directory. In mix.exs
you should have a function like this:
defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
So without placing your file in proper directory, it won't be loaded.
Upvotes: 1