【问题标题】:How to extend the TestEnvironment of a ZIO Test如何扩展 ZIO 测试的 TestEnvironment
【发布时间】:2020-02-03 18:35:49
【问题描述】:

我想测试以下功能:

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?

【问题讨论】:

    标签: scala zio zio-test


    【解决方案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_
    }
    

    【讨论】:

      【解决方案2】:

      这是我想出来的:

      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()
            })
          }
      

      使用TestEnvironmentprovideSome 来设置我的环境。

      【讨论】:

        猜你喜欢
        • 2020-04-14
        • 2011-02-21
        • 2010-11-25
        • 1970-01-01
        • 1970-01-01
        • 2012-12-23
        • 2016-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多