2016-11-26 62 views
0

我正在开发一个phoenix应用程序。此应用程序是伞应用程序的一部分。在这把伞我有责任对申请不同的领域,这是小应用程序:集成测试Web应用程序在伞中的问题

  • 凤凰网页API(“API”)
  • 核心业务逻辑(“核心”)
  • 用户认证(” AUTH “)
  • 数据库架构(” DB“)

”API“ 取决于这两个 ”核心“ 和 ”权威性“,而这两个应用程序依赖于 ”DB“。

只有“db”应用程序有ecto回购,其他所有应用程序都没有。回购由“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”项目的应用程序上遇到此错误时,我能够应用“共享模式”解决方案。

我的问题是我可以如何通过我的“api”集成测试解决所有权问题。

回答

1

下面是运行在伞模式测试(如错误消息所描述的)几个注意事项

  1. 你的依赖回购需要“签出”
  2. 你依赖的回购可能永远不会开始
  3. 你的依赖回购可能无法在“共享”模式

从那里,也许你的test_helper.exs力量看起来是这样的(伪)运行:

ExUnit.start 

Db.Repo.start_link() 
Core.Repo.start_link() 
Auth.Repo.start_link() 

Ecto.Adapters.SQL.Sandbox.checkout(Db.Repo) 
Ecto.Adapters.SQL.Sandbox.checkout(Core.Repo) 
Ecto.Adapters.SQL.Sandbox.checkout(Auth.Repo) 

Ecto.Adapters.SQL.Sandbox.mode(Api.Repo, :manual) 
Ecto.Adapters.SQL.Sandbox.mode(Db.Repo, :shared) 
Ecto.Adapters.SQL.Sandbox.mode(Core.Repo, :shared) 
Ecto.Adapters.SQL.Sandbox.mode(Auth.Repo, :shared) 

更新:

不要忘记在mix.exs

defp deps do 
    [ 
     ... 
     {:db, path: "path/to/db"}, 
     ... 
    ] 
end 
+0

DB的项目路径谢谢您的回答。对此的一些想法: (1)只有一个Repo,即“db”应用程序之一。回购是受监督的,应该在“db”应用程序启动时启动。 (2)当我为“api”编写测试时,“api”应用程序不知道有一个ecto回购,因为“api”没有直接依赖“db”。 这就是为什么我不知道如何在我的“应用程序”测试中签出回购协议的原因。 我在我的问题中包含这个 – Thorakas

+0

@Thorakas我的答案仍然存在,测试必须启动另一个otp应用程序。不确定您是否处于异步模式 – ardhitama

+0

这些测试不以异步模式运行。我还配置了“api”项目,要求启动“核心”和“身份验证”,它们本身需要启动“db”。正如我之前所说,我不知道如何将你的答案应用于我的情况。我无法检查出'Db.Repo',因为这个模块在“api”项目中是未知的。 – Thorakas

相关问题