Scala Zio测试断言IO

发布于 2025-02-04 18:55:08 字数 1269 浏览 4 评论 0原文

我的功能具有RETOR类型RIO的功能,如下所示:

object Compresser {
  val buffer = ZManaged.fromAutoCloseable(ZIO.succeed(new ByteArrayOutputStream()))

  val stream = for {
    b <- buffer
    s <- ZManaged.fromAutoCloseable(ZIO.succeed(new DeflaterOutputStream(b, false)))
  } yield (b, s)

  def compress(input: Array[Byte]): RIO[blocking.Blocking, Array[Byte]] = stream.use { case (buffer, stream) =>
    for {
      () <- blocking.effectBlocking(stream.write(input))
      () <- blocking.effectBlocking(stream.flush())
      result = buffer.toByteArray
    } yield result
  }
}

我在为此功能编写单元测试时遇到了麻烦,是否有任何方法可以检查返回的结果是可抛出的或数组[字节],并将实际结果与预期的结果进行比较?这样的事情:

object CompresserSpec extends DefaultRunnableSpec{

  def spec = suite("Compresser")(
    test("Compress an empty string") {
      val input = ""
      val expected = Array[Byte](0, 0, 0, -1, -1, 3, 0)
      val output: IO[Throwable, Array[Byte]] = compress(input.getBytes("UTF-8")).provide(Has(Blocking))
      //Something like: assert(output)(equalTo(expected))
    },
    test("Compress null") {
      val output: IO[Throwable, Array[Byte]] = compress(null).provide(Has(Blocking))
      //Something like: assert(output)(NullPointerException)
    } 
  )
}

I have a function with return type RIO as follows:

object Compresser {
  val buffer = ZManaged.fromAutoCloseable(ZIO.succeed(new ByteArrayOutputStream()))

  val stream = for {
    b <- buffer
    s <- ZManaged.fromAutoCloseable(ZIO.succeed(new DeflaterOutputStream(b, false)))
  } yield (b, s)

  def compress(input: Array[Byte]): RIO[blocking.Blocking, Array[Byte]] = stream.use { case (buffer, stream) =>
    for {
      () <- blocking.effectBlocking(stream.write(input))
      () <- blocking.effectBlocking(stream.flush())
      result = buffer.toByteArray
    } yield result
  }
}

I am having trouble writing a unit test for this function, is there any way to check if the returned result is a Throwable or Array[Byte] and compare the actual result with the expected? Something like this:

object CompresserSpec extends DefaultRunnableSpec{

  def spec = suite("Compresser")(
    test("Compress an empty string") {
      val input = ""
      val expected = Array[Byte](0, 0, 0, -1, -1, 3, 0)
      val output: IO[Throwable, Array[Byte]] = compress(input.getBytes("UTF-8")).provide(Has(Blocking))
      //Something like: assert(output)(equalTo(expected))
    },
    test("Compress null") {
      val output: IO[Throwable, Array[Byte]] = compress(null).provide(Has(Blocking))
      //Something like: assert(output)(NullPointerException)
    } 
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2025-02-11 18:55:08

事实证明,我需要的只是一个测试,带有用于理解的

object ZDeflaterSpec extends DefaultRunnableSpec{

  def spec : ZSpec[Environment, Failure] = suite("Compresser")(
    testM("Compress an empty string") {
      val input = ""
      val expected = Array[Byte](0, 0, 0, -1, -1, 3, 0)
      for {
        output <- compress(input.getBytes("UTF-8")).provide(Has(Blocking.Service.live))
      } yield assert(output)(equalTo(expected))
    },

Turns out what I need is just a testM comes with a for comprehension

object ZDeflaterSpec extends DefaultRunnableSpec{

  def spec : ZSpec[Environment, Failure] = suite("Compresser")(
    testM("Compress an empty string") {
      val input = ""
      val expected = Array[Byte](0, 0, 0, -1, -1, 3, 0)
      for {
        output <- compress(input.getBytes("UTF-8")).provide(Has(Blocking.Service.live))
      } yield assert(output)(equalTo(expected))
    },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文