在scalatest中创建和删除scala光滑表之前和之后的异步

Chr*_*art 7 scala scalatest slick slick-3.0

我试图找到一种方法来获得异步beforeafter语句,其中下一个测试用例在测试用例内部的操作完成之前不会运行.就我而言,它是在数据库中创建和删除表

  val table = TableQuery[BlockHeaderTable]
  val dbConfig: DatabaseConfig[PostgresDriver] = DatabaseConfig.forConfig("databaseUrl")
  val database: Database = dbConfig.db
  before {
    //Awaits need to be used to make sure this is fully executed before the next test case starts
    //TODO: Figure out a way to make this asynchronous 
    Await.result(database.run(table.schema.create), 10.seconds)
  }

  "BlockHeaderDAO" must "store a blockheader in the database, then read it from the database" in {
    //...
  }

  it must "delete a block header in the database" in {
    //...
  }

  after {
    //Awaits need to be used to make sure this is fully executed before the next test case starts
    //TODO: Figure out a way to make this asynchronous
    Await.result(database.run(table.schema.drop),10.seconds)
  }
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法可以删除Awaitbeforeafter函数内部的这些调用?

Gal*_*Gal 5

不幸的是,@Jeffrey Chung 的解决方案对我来说是悬而未决的(因为futureValue实际上在内部等待)。这就是我最终做的:

import org.scalatest.{AsyncFreeSpec, FutureOutcome}
import scala.concurrent.Future

class TestTest extends AsyncFreeSpec /* Could be any AsyncSpec. */ {
  // Do whatever setup you need here.
  def setup(): Future[_] = ???
  // Cleanup whatever you need here.
  def tearDown(): Future[_] = ???
  override def withFixture(test: NoArgAsyncTest) = new FutureOutcome(for {
    _ <- setup()
    result <- super.withFixture(test).toFuture
    _ <- tearDown()
  } yield result)
}
Run Code Online (Sandbox Code Playgroud)