首页 文章

在伞中集成测试Web应用程序的问题

提问于
浏览
0

我正在研究凤凰应用程序 . 此应用程序是伞形应用程序的一部分 . 在这个保护伞中,我有一些小应用程序负责应用程序的不同区域,它们是:

  • phoenix web api("api")

  • 核心业务逻辑("core")

  • 用户身份验证("auth")

  • 数据库架构("db")

“api”依赖于“core”和“auth”,而这两个应用程序依赖于“db” .

只有“db”应用程序有一个ecto 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”集成测试解决所有权问题 .

1 回答

  • 1

    以下是一些在伞形模式下运行测试的警告(如错误消息中所述)

    • 您的受托回购必须是"checked out"

    • 您的依赖回购可能永远不会开始

    • 您的从属回购可能无法在"shared"模式下运行

    从那里,也许你的 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)
    

    Update:

    Don 't forget to include DB'的项目路径 mix.exs

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

相关问题