Home > database >  Scala ZIO test assert an IO
Scala ZIO test assert an IO

Time:06-13

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

CodePudding user response:

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))
    },
  • Related