0

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

此代码编译:

  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 Test的限制还是我错过了什么?

4

1 回答 1

4

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

于 2020-04-10T14:04:02.690 回答