gpt4 book ai didi

scala - 使用 cats-effect 的 IO monad 进行单元测试

转载 作者:行者123 更新时间:2023-12-02 03:56:26 24 4
gpt4 key购买 nike

场景

在我当前正在编写的应用程序中,我正在使用 cats-effect 的 IO monadIOApp .

如果从命令行参数“debug”开始,我会将程序流委托(delegate)到调试循环中,该循环等待用户输入并执行各种与调试相关的方法。一旦开发人员在没有任何输入的情况下按下 enter,应用程序就会退出调试循环并退出 main 方法,从而关闭应用程序。

这个应用程序的主要方法大致如下:

import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
import cats.effect.{ExitCode, IO, IOApp}
import cats.implicits._

object Main extends IOApp {

val BlockingFileIO: ExecutionContextExecutor = ExecutionContext.fromExecutor(blockingIOCachedThreadPool)

def run(args: List[String]): IO[ExitCode] = for {
_ <- IO { println ("Running with args: " + args.mkString(","))}
debug = args.contains("debug")
// do all kinds of other stuff like initializing a webserver, file IO etc.
// ...
_ <- if(debug) debugLoop else IO.unit
} yield ExitCode.Success

def debugLoop: IO[Unit] = for {
_ <- IO(println("Debug mode: exit application be pressing ENTER."))
_ <- IO.shift(BlockingFileIO) // readLine might block for a long time so we shift to another thread
input <- IO(StdIn.readLine()) // let it run until user presses return
_ <- IO.shift(ExecutionContext.global) // shift back to main thread
_ <- if(input == "b") {
// do some debug relevant stuff
IO(Unit) >> debugLoop
} else {
shutDown()
}
} yield Unit

// shuts down everything
def shutDown(): IO[Unit] = ???
}

现在,我想测试一下是否我的 run 方法的行为与我的 ScalaTest 中预期的一样:

import org.scalatest.FlatSpec

class MainSpec extends FlatSpec{

"Main" should "enter the debug loop if args contain 'debug'" in {
val program: IO[ExitCode] = Main.run("debug" :: Nil)
// is there some way I can 'search through the IO monad' and determine if my program contains the statements from the debug loop?
}
}
<小时/>

我的问题

我能否以某种方式“搜索/迭代 IO monad”并确定我的程序是否包含调试循环中的语句?我是否必须调用 program.unsafeRunSync() 来检查它?

最佳答案

您可以在自己的方法中实现 run 的逻辑,并进行测试,在返回类型上不受限制并将 run 转发到您自己的方法执行。由于 run 强制您执行 IO[ExitCode],因此您无法从返回值中表达太多内容。一般来说,无法“搜索”IO 值,因为它只是描述具有副作用的计算的值。如果您想检查它的基础值,可以通过在世界末日(您的 main 方法)中运行它来实现,或者为了进行测试,您可以 unsafeRunSync 它。

例如:

sealed trait RunResult extends Product with Serializable
case object Run extends RunResult
case object Debug extends RunResult

def run(args: List[String]): IO[ExitCode] = {
run0(args) >> IO.pure(ExitCode.Success)
}

def run0(args: List[String]): IO[RunResult] = {
for {
_ <- IO { println("Running with args: " + args.mkString(",")) }
debug = args.contains("debug")
runResult <- if (debug) debugLoop else IO.pure(Run)
} yield runResult
}

def debugLoop: IO[Debug.type] =
for {
_ <- IO(println("Debug mode: exit application be pressing ENTER."))
_ <- IO.shift(BlockingFileIO) // readLine might block for a long time so we shift to another thread
input <- IO(StdIn.readLine()) // let it run until user presses return
_ <- IO.shift(ExecutionContext.global) // shift back to main thread
_ <- if (input == "b") {
// do some debug relevant stuff
IO(Unit) >> debugLoop
} else {
shutDown()
}
} yield Debug

// shuts down everything
def shutDown(): IO[Unit] = ???
}

然后在您的测试中:

import org.scalatest.FlatSpec

class MainSpec extends FlatSpec {

"Main" should "enter the debug loop if args contain 'debug'" in {
val program: IO[RunResult] = Main.run0("debug" :: Nil)
program.unsafeRunSync() match {
case Debug => // do stuff
case Run => // other stuff
}
}
}

关于scala - 使用 cats-effect 的 IO monad 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54767360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com