【发布时间】:2016-03-26 00:38:03
【问题描述】:
我正在尝试在 tdd 中做一个小集成测试,以获得我一直在修改的想法。
我不是 TDD 新手,但也不是专家。我昨天才在 ScalaTest 中开始使用它,所以我在这里。
这是有效的:
class BookTest extends FlatSpec with Matchers with ScalaFutures with BeforeAndAfter
{
implicit val defaultPatience =
PatienceConfig(timeout = Span(5, Seconds), interval = Span(500, Millis))
val db = Database.forURL("jdbc:sqlite:/home/teolha/test.sqlite", driver = "org.sqlite.JDBC")
private val books = Books.all
val setup = DBIO.seq(
(books.schema).create,
// Insert some dummy data
books += new Book(-1, 0, "Awesome Title #1"),
books += new Book(-1, 1, "Gorgeous Sequel"),
books += new Book(-1, 2, "Mehmeh Prequel")
)
db.run(setup)
"Books" should "be a Sequences of Books" in
{
val bookList: Future[Seq[Book]] = db.run(books.result)
whenReady(bookList)
{
result => {
result shouldBe a [Seq[_]] // Remember type erasure
result(0) shouldBe a [Book]
}
}
}
"Books" should "contain the books we inserted" in
{
val bookList = db.run(books.result)
whenReady(bookList)
{
result =>
{
result should have length 3
(result(0).title shouldEqual "Awesome Title #1") (after being lowerCased)
(result(1).title shouldEqual "Gorgeous Sequel") (after being lowerCased)
(result(2).title shouldEqual "Mehmeh Prequel") (after being lowerCased)
}
}
}
}
现在我在这个设置中遇到了多个问题:
- 我更喜欢内存数据库
- 我想在每次测试后删除所有表并设置它们 在下一个之前再次启动并在特定测试中进行插入
所以我尝试了这样的事情:
class BookTest extends FlatSpec with Matchers with ScalaFutures with BeforeAndAfter {
implicit val defaultPatience =
PatienceConfig(timeout = Span(5, Seconds), interval = Span(500, Millis))
val db = Database.forURL("jdbc:sqlite:/home/teolha/test.sqlite", driver = "org.sqlite.JDBC")
private val books = Books.all
before {
db.run(setup)
}
after {
db.run(tearDown)
}
val setup = DBIO.seq(
(books.schema).create
)
val tearDown = DBIO.seq(
(books.schema).drop
)
"Books" should "be a Sequences of Books" in {
// Insert some dummy data
db.run(
DBIO.seq(
books += new Book(-1, 0, "Awesome Title #1"),
books += new Book(-1, 1, "Gorgeous Sequel"),
books += new Book(-1, 2, "Mehmeh Prequel")
)
)
val bookList: Future[Seq[Book]] = db.run(books.result)
whenReady(bookList) {
result => {
result shouldBe a[Seq[_]] // Remember type erasure
result(0) shouldBe a[Book]
}
}
}
}
理论上这应该可行,不是吗?好吧,它没有。测试后还有一个表格,里面还有数据。
如何正确地做到这一点?之后我想把它放在内存数据库中(如前所述),但目前为了调试它更有意义。
【问题讨论】:
标签: scala integration-testing scalatest