【问题标题】:Cyclic dependency in test cases测试用例中的循环依赖
【发布时间】:2018-09-24 01:20:01
【问题描述】:

我在伞形项目设置中有三个应用程序

  1. 数据层
  2. ApiLayer
  3. OtherLayer 一些其他层(但必须)

现在我在混合文件中定义了这样的依赖项 应用 ApiLayer

{:OtherLayer, in_umbrella: true},
{:Datalayer, in_umbrella: true}

App Datalayer 不依赖伞形应用 应用OtherLayer

{:Datalayer, in_umbrella: true}

但是从应用 ApiLayer 调用的测试用例需要调用 Datalayer 中定义的上下文,而 Datalayer 的上下文又需要访问OtherLayer 的一些功能,但不能。因为如果我在 Datalayer 中添加它的依赖项。这将是循环依赖情况。我需要通过我的测试用例。有没有办法在不创建循环依赖的情况下加载一些代码块? 任何帮助将不胜感激。谢谢

【问题讨论】:

  • 我尝试了一些解决方案,例如动态要求该模块但没有奏效,并尝试了 test_helper 中的 ensure_all_started 选项,但得到了 {:error, {:m, {:invalid_restart_type, :data_layer}}}跨度>
  • 如果它们真的如此集成,它们可能不应该是单独的应用程序。一种解决方案可能是通过将模块传递给函数来处理这种通信。例如,如果您的 ApiLayer 中有一个调用 DataLayer 的方法,但 DataLayer 还需要来自 OtherLayer 的值,您可以在 DataLayer 中设计函数以接受模块作为参数并将其传递给 ApiLayer。 def my_func(x), do: DataLayer.Module.func(x, OtherLayer.DependencyModule)

标签: dependencies elixir phoenix-framework ex-unit


【解决方案1】:

如果OtherLayer 始终包含在已将Datalayer 作为依赖项的应用程序中,那么您可以从OtherLayer 中删除Datalayer 依赖项,因为无论如何该应用程序都将包含在您的版本中。如果您需要将 OtherLayer 构建为独立应用程序,请创建一个 OtherLayerWrapper 应用程序,该应用程序具有 DatalayerOtherLayer 作为依赖项。

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2023-04-05
    • 2012-10-21
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2013-02-19
    • 2020-03-19
    相关资源
    最近更新 更多