请注意,此答案适用于 RC17,并将在 RC18 中发生显着变化。你是对的,就像在其他组合环境的情况下,我们需要实现一个函数来从我们拥有的模块构建我们的整个环境。Spec 内置了几个组合provideManaged
器来执行此操作,因此您无需在测试本身中执行此操作。所有这些都具有“正常”变体,可为套件中的每个测试提供单独的环境副本,以及“共享”变体,当创建成本高昂的资源时,将为整个套件创建一个环境副本就像 Kafka 服务一样。
您可以在下面看到一个provideSomeManaged
用于提供将测试环境扩展到测试的环境的示例。
在 RC18 中,将提供与 ZIO 等效的各种其他提供变体以及新的层概念,以使为 ZIO 应用程序构建组合环境变得更加容易。
import zio._
import zio.clock._
import zio.test._
import zio.test.environment._
import ExampleSpecUtil._
object ExampleSpec
extends DefaultRunnableSpec(
suite("ExampleSpec")(
testM("My Test") {
for {
time <- clock.nanoTime
_ <- Logging.logLine(
s"The TestClock says the current time is $time"
)
} yield assertCompletes
}
).provideSomeManaged(testClockWithLogging)
)
object ExampleSpecUtil {
trait Logging {
def logging: Logging.Service
}
object Logging {
trait Service {
def logLine(line: String): UIO[Unit]
}
object Live extends Logging {
val logging: Logging.Service =
new Logging.Service {
def logLine(line: String): UIO[Unit] =
UIO(println(line))
}
}
def logLine(line: String): URIO[Logging, Unit] =
URIO.accessM(_.logging.logLine(line))
}
val testClockWithLogging
: ZManaged[TestEnvironment, Nothing, TestClock with Logging] =
ZIO
.access[TestEnvironment] { testEnvironment =>
new TestClock with Logging {
val clock = testEnvironment.clock
val logging = Logging.Live.logging
val scheduler = testEnvironment.scheduler
}
}
.toManaged_
}