如何在每次测试之后或之前使用inMemoryDatabase重置FakeApplication中的数据库?我希望每次测试后数据库的状态都返回到初始状态。回滚或清除数据库并再次运行SetupData()就可以了。
我尝试了很多不同的东西。在每次测试之间永远不会重置数据库。我该怎么做呢? 运行以下测试套件时,数据库的状态将继续从测试到测试。因此,如果我在Test2中修改了一些数据,那么它正在影响Test3。
trait TestUtil {
/**
* Executes a block of code in a running application.
*/
def running[T](fakeApp: FakeApplication)(block: => T): T = {
synchronized {
try {
Play.start(fakeApp)
setup_data()
block
} finally {
Play.stop()
}
}
}
def setup_data() = {
//add data to the database
}
}
我尝试使用上述特性让Play App在每次测试后停止,但它没有帮助。
@RunWith(classOf[JUnitRunner])
class ConditionTests2 extends Specification with TestUtil {
sequential // Make the tests run sequentially instead of in parallel
// test1
"element with id 1" should {
"should have name PETER" in {
running(new FakeApplication(additionalConfiguration = inMemoryDatabase("test"))) {
// name should be "PETER"
}
}
}
// test2
"renaming element with id 1" should {
"successfully modify element 1" in {
running(new FakeApplication(additionalConfiguration = inMemoryDatabase("test"))) {
// Set Name = "John"
}
}
}
// test3
"element with id 1" should {
"should have name PETER" in {
running(new FakeApplication(additionalConfiguration = inMemoryDatabase("test"))) {
// name should be "PETER"
// fails because name is now JOHN
}
}
}
}