DBIOAction cleanUp 或 asTry 不会导致内部执行

DBIOAction cleanUp or asTry do not lead to internal execution

我正在尝试测试副作用是否被执行。都没有

DBIOAction.successful(()).cleanUp(_.fold {
  println("yeay!")
  DBIOAction.successful(())
} { _ => 
  println("aww.")
  DBIOAction.successful(())
})

也不

DBIOAction.successful(()).asTry.map {
  case Succeed(_) => println("yeay!")
  case Failure(_) => println("aww.")
}

打印任何东西。我对 Slick 不太熟悉,但它可能需要将 run 放在某个地方。有没有办法提供用于测试目的的轻量级运行时?

咨询the documentation of I/O actions

Operations that can be executed on a database are called database I/O actions (DBIOAction). Several operations on queries and tables create I/O actions, for example myQuery.result, myQuery.result.headOption, myQuery += data or myTable.schema.create. Actions can be composed with combinators like andThen, flatMap, DBIO.seq or transactionally.

Just like a query, an I/O action is only a description of an operation. Creating or composing actions does not execute anything on a database. Combined actions always consist of strictly linear sequences of other actions. Parts of an action never run concurrently.

about results:

Any action can be run on a database to obtain the results (or perform side effects such as updating the database). Execution is always asynchronous, i.e. it does not block the caller thread. Any kind of action can be run to obtain a Future that is eventually completed with a result when the execution is finished (myDatabase.run(myAction)). Actions that produce a sequence of values usually support streaming results as well. Such an action can be combined with a database to produce a Reactive Streams Publisher (myDatabase.stream(myAction)). The action is executed when a consumer subscribes to the Publisher.

您必须 database.run(ioAction) 评估任何副作用(包括 println)。