1

我想测试以下功能:

def curl(host: String, attempt: Int = 200): ZIO[Loggings with Clock, Throwable, Unit]

如果环境只使用标准 ZIO 环境,例如Console with Clock,则测试将开箱即用:

testM("curl on valid URL") {
      (for {
        r <- composer.curl("https://google.com")
      } yield
        assert(r, isUnit))
    }

测试环境将由zio-test.

所以问题是,如何用我的Loggings模块扩展 TestEnvironment?

4

2 回答 2

1

这就是我想出的:

testM("curl on valid URL") {
      (for {
        r <- composer.curl("https://google.com")
      } yield
        assert(r, isUnit))
        .provideSome[TestEnvironment](env => new Loggings.ConsoleLogger
           with TestClock {
           override val clock: TestClock.Service[Any] = env.clock
           override val scheduler: TestClock.Service[Any] = env.scheduler
           override val console: TestLogger.Service[Any] = MyLogger()
      })
    }

使用TestEnvironmentwithprovideSome设置我的环境。

于 2020-02-03T18:35:49.470 回答
1

请注意,此答案适用于 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_
}
于 2020-02-03T19:18:17.197 回答