异步断言未在 scalatest 中触发
Asynchronous assertion is not firing in scalatest
我正在尝试编写一个测试来检查是否设置了正确的数据库,但是断言永远不会触发,并且一切都成功结束(即使它应该失败):
import anorm._
import org.scalatestplus.play._
import play.api.db.DB
class Housekeeping extends PlaySpec with OneServerPerSuite {
// Make sure the test database is loaded
"test connection to test database" in {
DB.withConnection { implicit connection =>
SQL("SELECT * FROM accounts WHERE ID = 1").withResult(res => {
val row = res.head.row
val name = row.asMap("accounts.name")
println(name) // Prints to console
name mustEqual "this should fail"
println("HERE") // Never prints to console
})
}
}
}
控制台:
[info] Housekeeping:
[info] - application - Creating Pool for datasource 'default'
tyler
[info] - test connection to test database
[info] - application - Shutting down connection pool.
我不确定为什么什么都没有发生,因为我从数据库中得到的名字很好。我找不到任何关于执行异步测试的文档,我认为这可能是问题的一部分。
类似 this 的东西可以帮助:
import org.scalatest._
import concurrent.AsyncAssertions
import anorm._
import org.scalatestplus.play._
import play.api.db.DB
class Housekeeping extends PlaySpec with OneServerPerSuite with AsyncAssertions {
// Make sure the test database is loaded
"test connection to test database" in {
val w = new Waiter
DB.withConnection { implicit connection =>
SQL("SELECT * FROM accounts WHERE ID = 1").withResult(res => {
val row = res.head.row
val name = row.asMap("accounts.name")
println(name) // Prints to console
w { name mustEqual "this should fail" }
w.dismiss()
println("HERE")
})
}
w.await()
}
}
您的问题是 scalatest 没有收到 mustEqual
引发的异常,因为它是异步抛出的。
实际上 Waiter
是 Promise
的一种包装器(所以你必须做 dismiss()
才能完成它)并且 w.await()
只是等待它并且将异常从 w{...}
重定向到 scalatest 的线程。
我正在尝试编写一个测试来检查是否设置了正确的数据库,但是断言永远不会触发,并且一切都成功结束(即使它应该失败):
import anorm._
import org.scalatestplus.play._
import play.api.db.DB
class Housekeeping extends PlaySpec with OneServerPerSuite {
// Make sure the test database is loaded
"test connection to test database" in {
DB.withConnection { implicit connection =>
SQL("SELECT * FROM accounts WHERE ID = 1").withResult(res => {
val row = res.head.row
val name = row.asMap("accounts.name")
println(name) // Prints to console
name mustEqual "this should fail"
println("HERE") // Never prints to console
})
}
}
}
控制台:
[info] Housekeeping:
[info] - application - Creating Pool for datasource 'default'
tyler
[info] - test connection to test database
[info] - application - Shutting down connection pool.
我不确定为什么什么都没有发生,因为我从数据库中得到的名字很好。我找不到任何关于执行异步测试的文档,我认为这可能是问题的一部分。
类似 this 的东西可以帮助:
import org.scalatest._
import concurrent.AsyncAssertions
import anorm._
import org.scalatestplus.play._
import play.api.db.DB
class Housekeeping extends PlaySpec with OneServerPerSuite with AsyncAssertions {
// Make sure the test database is loaded
"test connection to test database" in {
val w = new Waiter
DB.withConnection { implicit connection =>
SQL("SELECT * FROM accounts WHERE ID = 1").withResult(res => {
val row = res.head.row
val name = row.asMap("accounts.name")
println(name) // Prints to console
w { name mustEqual "this should fail" }
w.dismiss()
println("HERE")
})
}
w.await()
}
}
您的问题是 scalatest 没有收到 mustEqual
引发的异常,因为它是异步抛出的。
实际上 Waiter
是 Promise
的一种包装器(所以你必须做 dismiss()
才能完成它)并且 w.await()
只是等待它并且将异常从 w{...}
重定向到 scalatest 的线程。