用 BeforeAndAfter 测试,table 放在后面,table 还在
Test with BeforeAndAfter, table drop in after, table still there
我正尝试在 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)
}
}
}
}
现在这个设置有多个问题:
- 我更喜欢内存数据库
- 我想在每次测试后删除所有 tables 并设置它们
在下一个之前再次向上并在特定测试中进行插入
所以我尝试了这样的事情:
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]
}
}
}
}
理论上这应该可行,不是吗?好吧,事实并非如此。测试后还有一个table,里面还是填满了数据
如何正确地做到这一点?之后我想把它放在一个内存数据库中(如上所述),但目前为了调试它更有意义。
其实我的做法是对的。唯一的问题是超时时间仍然设置得太低。我认为 5 秒就足够了,但我将其增加到 10 秒,现在一切正常!
我正尝试在 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)
}
}
}
}
现在这个设置有多个问题:
- 我更喜欢内存数据库
- 我想在每次测试后删除所有 tables 并设置它们 在下一个之前再次向上并在特定测试中进行插入
所以我尝试了这样的事情:
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]
}
}
}
}
理论上这应该可行,不是吗?好吧,事实并非如此。测试后还有一个table,里面还是填满了数据
如何正确地做到这一点?之后我想把它放在一个内存数据库中(如上所述),但目前为了调试它更有意义。
其实我的做法是对的。唯一的问题是超时时间仍然设置得太低。我认为 5 秒就足够了,但我将其增加到 10 秒,现在一切正常!