gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:46:11 27 4
gpt4 key购买 nike

我正在想办法让异步 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)
}

有没有一种简单的方法可以删除这些 Await在我的内部调用 beforeafter职能?

最佳答案

不幸的是,@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)
}

关于scala - 在 scalatest 中创建和删除 scala 光滑表之前和之后的异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39421757/

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