为什么无法在 ZIO 测试中提取变量中的图层

Why is it not possible to extract the Layers in a variable in ZIO Test

在为我的测试提供层时,我偶然发现了这种奇怪的行为,即无法提取值中的层。

此代码编译:

  def spec: ZSpec[environment.TestEnvironment, Any] =
    suite("EnvironmentLoaderSuites")(
        testM("the Config is correct") {
          assertM(environmentLoader.bpfEnv())(
            equalTo(expectedEnv))
        }
    ).provideCustomLayer(Console.live >>> loggings.consoleLogger >>> environmentLoader.live(testEnvPath))

当我现在像这样提取图层时:

  val layers = Console.live >>> loggings.consoleLogger >>> environmentLoader.live(testEnvPath)
  def spec: ZSpec[environment.TestEnvironment, Any] =
    suite("EnvironmentLoaderSuites")(
        testM("the Config is correct") {
          assertM(environmentLoader.bpfEnv())(
            equalTo(expectedEnv))
        }
    ).provideCustomLayer(layers)

我得到以下编译异常:

Error:(48, 26) type mismatch;
 found   : zio.ZLayer[Any,Throwable,finnova.bpf.client.environmentLoader.EnvironmentLoader]
    (which expands to)  zio.ZLayer[Any,Throwable,zio.Has[finnova.bpf.client.environmentLoader.Service]]
 required: zio.ZLayer[zio.test.environment.TestEnvironment,zio.test.TestFailure[Any],?]
    (which expands to)  zio.ZLayer[zio.Has[zio.clock.Clock.Service] with zio.Has[zio.console.Console.Service] with zio.Has[zio.system.System.Service] with zio.Has[zio.random.Random.Service] with zio.Has[zio.blocking.Blocking.Service] with zio.Has[zio.test.Annotations.Service] with zio.Has[zio.test.environment.TestClock.Service] with zio.Has[zio.test.environment.TestConsole.Service] with zio.Has[zio.test.environment.Live.Service] with zio.Has[zio.test.environment.TestRandom.Service] with zio.Has[zio.test.Sized.Service] with zio.Has[zio.test.environment.TestSystem.Service],zio.test.TestFailure[Any],?]
    ).provideCustomLayer(layers)

这是 ZIO 测试 的限制还是我错过了什么?

我认为您需要将层的故障类型提升到 TestFailure。您可以使用 layer.mapError(TestFailure.fail) 执行此操作。当您直接提供它时,编译器可能已经扩大了类型,因为它还不完全已知,而现在由于您已经定义了一个中间体val,类型已经完全确定。