2013-10-08 12 views
5

Sto provando a eseguire alcuni test per la mia applicazione. Dovrebbe essere possibile eseguire test in un nuovo database di memoria nuovo ma non lo farò funzionare.Esegui test in memoria db play framework

La mia prova assomiglia a questo ora:

"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 protected]", "password" -> "test123", "gender" -> "male", "birthdate" -> "Oct 1, 2013", "nationality" -> "Sweden")).get 
    val add2 = route(FakeRequest(POST, "/rest/personaluser").withFormUrlEncodedBody("name" -> "user2" , "email" -> "[email protected]", "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 protected]") must beTrue 
    personalUserRepository.checkIfPersonalUserExists("[email protected]") must beTrue 

    //Verify user don't exist 
    personalUserRepository.checkIfPersonalUserExists("[email protected]") must beFalse 

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

    } 
} 
} 

Quando ho eseguito questo ottengo l'errore InconsistentDatabase: Database 'default' is in inconsistent state!. È perché lo standard inMemoryDB potrebbe non supportare MySQL che ho usato per il database predefinito?

Tuttavia, ho provato ad aggiungere la memoryDB come questo, invece:

definito qui

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

e usarlo in questo modo:

"Server" should { 

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

Ma quando lo faccio come questo , non usa il db in memoria, il test fallisce a //Count users perché non è uguale a 2, ma 7. Usa comunque il vero database, non la mia nuova memoria fresca db che cerco di usare in questo FakeAp plicatura.

Cosa sto sbagliando o cosa mi manca?

Qualsiasi risposta che possa mettermi sulla strada giusta è molto apprezzata! Grazie!

risposta

0

Inizierò dicendo che non conosco bene Play. Non hai mai menzionato come hai configurato il driver JDBC. Sospetto che mentre è in modalità test, l'applicazione stia ancora utilizzando la classe di driver MySQL contro il DB di H2. Avrai anche bisogno del file jar H2 nel classpath di prova.