我正在编写Slick 3.1.1文档中的简单入门(http://slick.typesafe.com/doc/3.1.1/gettingstarted.html)示例。
我写了以下测试来断言咖啡的数量:
@Test def countCoffees() = {
// Read all coffees and print them to the console
val rf = db.run(coffees.result)
// Equivalent SQL code:
// select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES
val r = Await.result(rf, Duration.Inf)
assert(r.size == 5)
}
有时候测试通过,有时候结果如下:
[info] - countCoffees *** FAILED ***
[info] org.scalatest.junit.JUnitTestFailedError: Vector() had size 0 instead of expected size 5 (Test.scala:40)
TestSuite的定义如下:
class SlickScalaEjemplo extends FunSuite with SchemaEjemplo
SchemaEjemplo
如下
trait SchemaEjemplo extends FunSuite with SlickBase with BeforeAndAfter {
val setup = DBIO.seq(
// Create the tables, including primary and foreign keys
(suppliers.schema ++ coffees.schema).create,
// Insert some suppliers
suppliers += (101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"),
suppliers += (49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460"),
suppliers += (150, "The High Ground", "100 Coffee Lane", "Meadows", "CA", "93966"),
// Equivalent SQL code:
// insert into SUPPLIERS(SUP_ID, SUP_NAME, STREET, CITY, STATE, ZIP) values (?,?,?,?,?,?)
// Insert some coffees (using JDBC's batch insert feature, if supported by the DB)
coffees ++= Seq(
("Colombian", 101, 7.99, 0, 0),
("French_Roast", 49, 8.99, 0, 0),
("Espresso", 150, 9.99, 0, 0),
("Colombian_Decaf", 101, 8.99, 0, 0),
("French_Roast_Decaf", 49, 9.99, 0, 0)
)
// Equivalent SQL code:
// insert into COFFEES(COF_NAME, SUP_ID, PRICE, SALES, TOTAL) values (?,?,?,?,?)
)
val setupFuture = db.run(setup)
after {
db.close()
}
}
为什么此次测试的等待无法正常工作?
答案 0 :(得分:1)
问题在于我没有等待以下
val setupFuture = db.run(setup)
执行所以测试正在运行,而不期望创建或完成架构。
我按如下方式更改了测试:
test("countCoffees") {
setupFuture.map(x => {
// Read all coffees and print them to the console
val rf = db.run(coffees.result)
// Equivalent SQL code:
// select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES
val r = Await.result(rf, Duration.Inf)
assert(r.size == 5)
})
}
因此,在完整模式上执行不同Action
和db.run
的执行,现在测试都是绿色的。