5

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

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

"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 中。

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

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

4

1 回答 1

0

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

于 2013-10-08T12:13:11.567 回答