我有一个名为insert_user的方法,它在应用程序的其他部分工作正常但由于某些原因它在这种情况下未定义并且无法弄清楚为什么
** (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
这是代码:
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
这是t est/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
答案 0 :(得分:1)
如果您未在Rumbl.TestHelpers
宏块中的Rumbl.ModelCase
中导入using
,则必须在测试套件中明确添加它才能导入此函数。
它应该有所帮助,但除此之外 - 请检查您的TestHelpers
是否放在test/support
目录中。在mix.exs
中你应该有这样的函数:
defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
因此,如果不将文件放在正确的目录中,则无法加载。