Scala Zio测试断言IO
我的功能具有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我需要的只是一个测试,带有用于理解的
Turns out what I need is just a testM comes with a for comprehension