在内存db play框架中运行测试

时间:2013-10-08 08:07:13

标签: scala testing playframework automated-tests specs2

我正在尝试为我的应用程序运行一些测试。 应该可以在一个全新的内存数据库中运行测试,但我不会让它工作。

我的测试现在看起来像这样:

"Server" should {

"persist data for personal user properly" in {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {

    //Create personal users
    val add1 = route(FakeRequest(POST, "/rest/personaluser").withFormUrlEncodedBody("name" -> "user1" , "email" -> "email@test1.com", "password" -> "test123", "gender" -> "male", "birthdate" -> "Oct 1, 2013", "nationality" -> "Sweden")).get
    val add2 = route(FakeRequest(POST, "/rest/personaluser").withFormUrlEncodedBody("name" -> "user2" , "email" -> "email@test2.com", "password" -> "test123", "gender" -> "male", "birthdate" -> "Oct 1, 2013","nationality" -> "Sweden")).get
    status(add1) must equalTo(OK)
    status(add2) must equalTo(OK)

    //Count users
    personalUserRepository.getAllPersonalUsers().length must beEqualTo(2)

    //Verify users exist
    personalUserRepository.checkIfPersonalUserExists("email@test1com") must beTrue
    personalUserRepository.checkIfPersonalUserExists("email@test2com") must beTrue

    //Verify user don't exist
    personalUserRepository.checkIfPersonalUserExists("email@test3com") must beFalse

    //Find user by email
    val findUser = route(FakeRequest(GET, "/rest/personaluserbyemail/email@test1.com")).get
    status(findUser) must equalTo(OK)
    contentAsString(findUser) must /("name" -> "user1")
    contentAsString(findUser) must /("email" -> "email@test1.com")
    contentAsString(findUser) must /("gender" -> "male")
    contentAsString(findUser) must /("nationality" -> "Sweden")
    contentAsString(findUser) must /("facebookID" -> "0")

  }
 }
}

当我运行此操作时,我收到错误InconsistentDatabase: Database 'default' is in inconsistent state!。 这是因为标准inMemoryDB可能不支持我用于默认数据库的MySQL吗?

但是,我尝试添加像这样的memoryDB:

在此定义

def memoryDB = Map("db.default.url" -> "jdbc:h2:mem:playdb;MODE=MYSQL;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;TRACE_LEVEL_SYSTEM_OUT=1")

并像这样使用它:

"Server" should {

"persist data for personal user properly" in {
  running(FakeApplication(additionalConfiguration = memoryDB)) {

但是当我这样做时,它不使用内存数据库,测试在//Count users失败,因为它不等于2,而是7.它无论如何都使用真实数据库,而不是我的我尝试在这个FakeApplication中使用新的内存数据库。

我做错了什么或我错过了什么?

任何可以让我走上正轨的答案都非常感谢! 谢谢!

1 个答案:

答案 0 :(得分:0)

我首先要说的是我对Play不熟悉。您从未提及如何配置JDBC驱动程序。我怀疑在测试模式下,应用程序仍在使用针对H2 DB的MySQL驱动程序类。您还需要测试类路径中的H2 jar文件。