1

私はフェニックスアプリケーションに取り組んでいます。このアプリケーションは、アンブレラ アプリの一部です。この傘には、アプリケーションのさまざまな領域を担当する小さなアプリケーションがあります。

  • フェニックス Web API (「API」)
  • コア ビジネス ロジック (「コア」)
  • ユーザー認証 (「auth」)
  • データベース スキーマ (「db」)

「api」は「core」と「auth」の両方に依存しますが、これら 2 つのアプリケーションは「db」に依存します。

「db」アプリだけが ecto リポジトリを持ち、他のすべてのアプリにはありません。リポジトリは「db」アプリによって開始され、監視されます。

ここで、「api」アプリケーションでコントローラーをテストしたいと思います。ここでectoの問題に遭遇します。コントローラーアクションをテストすると、このアクションは「auth」または「core」から関数を呼び出し、Repo「db」からの関数を呼び出します( など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」アプリケーションが「db」アプリケーションを認識していないため、接続チェックアウトを実行できないため、「api」テストで提案された解決策を使用してこのエラーを修正する方法がわからないことです。「db」プロジェクトに直接依存するアプリケーションでこのエラーが発生したとき、「共有モード」ソリューションを適用できました。

私の質問は、「api」統合テストで所有権の問題をどのように解決できるかということです。

4

1 に答える 1

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)

アップデート:

DB のプロジェクト パスを含めることを忘れないでください。mix.exs

defp deps do
   [
      ...
      {:db, path: "path/to/db"},
      ...
   ]
end
于 2016-11-27T15:52:49.583 に答える