当为我的测试提供Layers时,我偶然发现了这个奇怪的行为,它不可能在一个值中提取层。
这段代码可以编译。
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
类型完全确定。