【发布时间】:2017-04-10 14:19:09
【问题描述】:
我正在开发 phoenix 应用程序。此应用程序是伞形应用程序的一部分。在这个保护伞中,我有一些小型应用程序负责应用程序的不同领域,它们是:
- phoenix web api(“api”)
- 核心业务逻辑(“核心”)
- 用户身份验证(“auth”)
- 数据库架构(“db”)
“api”依赖于“core”和“auth”,而这两个应用程序依赖于“db”。
只有“db”应用有 ecto repo,所有其他应用都没有。该 repo 由“db”应用程序启动并受到监督。
现在我想在“api”应用程序中测试我的控制器。这就是我遇到 ecto 问题的地方。当我测试一个控制器动作时,这个动作将从“auth”或“core”调用一个函数,它从“db”调用Repo的函数(例如Repo.insert/2)。这导致OwnershipError:
** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.458.0>.
When using ownership, you must manage connections in one
of the three ways:
* By explicitly checking out a connection
* By explicitly allowing a spawned process
* By running the pool in shared mode
The first two options require every new process to explicitly
check a connection out or be allowed by calling checkout or
allow respectively.
The third option requires a {:shared, pid} mode to be set.
If using shared mode in tests, make sure your tests are not
async.
If you are reading this error, it means you have not done one
of the steps above or that the owner process has crashed.
See Ecto.Adapters.SQL.Sandbox docs for more information.
我现在的问题是我不知道如何使用“api”测试中建议的解决方案修复此错误,因为“api”应用程序不知道“db”应用程序,因此无法进行连接检查。当我在直接依赖于“db”项目的应用程序上遇到此错误时,我能够应用“共享模式”解决方案。
我的问题是如何通过“api”集成测试解决所有权问题。
【问题讨论】:
标签: elixir phoenix-framework ecto ex-unit