[英]Unit-testing with cats-effect's IO monad
在我目前正在編寫的應用程序中,我在IOApp中使用了cat -effect 的IO monad 。
如果從命令行參數“debug”開始,我會將我的程序流委派到一個調試循環中,該循環等待用戶輸入並執行各種與調試相關的方法。 一旦開發人員在沒有任何輸入的情況下按下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
s 中是否表現得像預期的那樣:
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
}
}
}
要搜索某個 monad 表達式,它必須是值,而不是語句,也就是具體化。 這就是(臭名昭著的)Free monad 背后的核心思想。 如果您要經歷在某些“代數”中表達您的代碼的麻煩,因為他們稱之為(想想 DSL)並通過Free
將其提升到 monad 表達式嵌套中,那么是的,您將能夠搜索它。 有很多資源可以比我更好地解釋 Free monads 谷歌是你的朋友。
我的一般建議是,良好測試的一般原則適用於任何地方。 隔離副作用部分並將其注入到主要邏輯部分,以便您可以在測試中注入假實現以允許各種斷言。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.