【发布时间】:2017-09-27 23:52:26
【问题描述】:
我目前正在测试使用函数 create_zone 的控制器,该函数依赖于检索用户以将所述用户与区域关联的函数,然后创建一个参与者条目,该条目只是两个条目的关联表。
def create_zone(attrs \\ %{}, user_id) do
user = Accounts.get_user!(user_id)
with{:ok, %Zone{} = zone} <- %Zone{}
|> Zone.changeset(attrs,user)
|> Repo.insert()
do
create_participant(zone,user)
end
end
我想使用 ExUnit 对其进行测试,但问题是测试框架试图搜索数据库中不存在的记录。
** (Ecto.NoResultsError) expected at least one result but got none in query:
from u in Module.Accounts.User,
where: u.id == ^1
我怎样才能模拟或创建它只是为了测试目的?
【问题讨论】:
-
是否用例如模拟它github.com/eproxus/meck 不适合你?该函数是公开的,所以它应该可以工作。
标签: testing mocking elixir phoenix-framework ecto