我尝试用scalatest测试我的play 2应用程序。 为了测试模型,我使用测试数据库。每次运行测试时,数据都会再次插入。所以我需要在测试后删除数据,甚至删除所有表。 我的代码是:
import models.User
import org.joda.time.DateTime
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import org.squeryl.adapters.PostgreSqlAdapter
import org.squeryl.{Session, SessionFactory}
import play.api.db.DB
import play.api.{Application, GlobalSettings}
import play.api.test.FakeApplication
class UserSpec extends PlaySpec with OneAppPerSuite with BeforeAndAfterAll {
implicit override lazy val app: FakeApplication = {
FakeApplication(
additionalConfiguration = Map(
"db.default.driver" -> "org.postgresql.Driver",
"db.default.url" -> "jdbc:postgresql://localhost:5432/test",
"db.default.user" -> "test",
"db.default.password" -> "test"),
withGlobal = Some(new GlobalSettings {
override def onStart(app: Application) = {
SessionFactory.concreteFactory = Some(() => Session.create(DB.getConnection()(app), new PostgreSqlAdapter))
}}
)
)}
override protected def afterAll(): Unit = {
}
"User" should {
"be creatable and saved to DB" in {
val user = User.createUser(
User("example1@example.com",
"John",
"Doe",
new DateTime(),
"afc677037be3d92324fa6597d6c1506b534e306b" // sha1("123456aA")
))
user.isPersisted mustBe true
}
}
}
在测试后是否有可能应用Downs进化?