testing - Phoenix 丹药 : mock internal functions

标签 testing mocking elixir phoenix-framework ecto

我目前正在测试使用函数 create_zone 的 Controller ,该函数依赖于检索用户以将所述用户与区域相关联的函数,然后创建参与者条目,该条目只是两个条目的关联表。

  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

我怎么能为了测试目的而模拟或创建它?

最佳答案

不要 mock 它,用 ex_machina 创建它:https://github.com/thoughtbot/ex_machina

Elixir 中不鼓励模拟:http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/ (你现在真的不需要阅读它,但如果你想模拟一些外部资源,请阅读它)。

关于testing - Phoenix 丹药 : mock internal functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46411134/

相关文章:

elixir - `priv`目录在混合伞项目中属于什么位置

join - 用于自引用关联的 Ecto 查询

testing - 移动应用程序的自动化跨平台测试

reactjs - 错误 : Unable to find an element with the text when I try a test with Jest in React

c# - 单元测试时 stub 与模拟

mongodb - 模拟官方 MongoDb 驱动程序

dictionary - Erlang/Elixir 合并带有大多数重复键的映射

react-native - 检查navigation.navigate是否在react Native测试中被调用

angularjs - 尝试测试工厂时遇到注入(inject)问题

java - 在 Java Web 应用程序中处理示例数据的最简单方法?