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

Unit-testing with cats-effect's IO monad

场景

在我目前正在编写的应用程序中,我正在使用 cats-effect 的 IO monad in an IOApp

如果使用命令行参数 '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 中表现得像预期的那样:

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

我的问题

我能否以某种方式 'search/iterate through the IO monad' 确定我的程序是否包含来自调试循环的语句?我必须打电话给 program.unsafeRunSync() 来检查吗?

要搜索一些 monad 表达式,它必须是值,而不是语句,也就是具体化的。这是(不)著名的 Free monad 背后的核心思想。如果你经历了在一些 "algebra" 中表达你的代码的麻烦,因为他们称之为(想想 DSL)它并通过 Free 将它提升到 monad 表达式嵌套中,那么是的你将能够搜索它。有很多资源比我能更好地解释 Free monads google 是你的朋友。

我的一般建议是良好测试的一般原则适用于任何地方。隔离副作用部分并将其注入到主要逻辑部分,以便您可以在测试中注入伪造的实现以允许各种断言。

您可以在您自己的方法中实现 run 的逻辑,并在您不受 return 类型限制并将 run 转发给您自己的地方进行测试执行。由于 run 迫使您的手牌达到 IO[ExitCode],因此您无法从 return 值中表达多少。一般来说,没有办法 "search" 一个 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
    }
  }
}