samedi 15 octobre 2016

Weird Scala test behavior: including a "println" makes it succeed

First, the whole code is in http://ift.tt/2drBwO6 I'm using a global object (which I don't know if it is Scala proper behavior) to share state in a Spray app. put adds to a Map, get obtains values from a map, thus:

path( Segment ) { quien =>
    get {
      println( Apuestas)
      val esta_apuesta = Apuestas.get( quien )
      complete( esta_apuesta )
    } 
  }

(please check the whole file at http://ift.tt/2eh53Yw)

The essential parts of the test code is

 "Crea apuestas correctamente" in {
      Put( "/0/2/Alguien") ~> myRoute ~> check {
    response.entity should not be equalTo(None)
    responseAs[String] must contain("Alguien")
      }

       Put( "/3/0/Menda") ~> myRoute ~> check {
    response.entity should not be equalTo(None)
    responseAs[String] must contain("Menda")
      }
    }

    "GET recupera apuesta correctamente" in {
      Get("/Alguien") ~> myRoute ~> check {
    response.entity should not be equalTo(None)
    responseAs[String] must contain("Alguien")
      }
    }

The problem is in the first chunk of code. If I comment out the println statement it does not work and it fails. This is the error message: [error] 'There was an internal server error.' doesn't contain 'Alguien' (MyServiceSpec.scala:49) Which obviously indicates that the "get" part hasn't worked yet, or it is in another thread, or I really don't have an idea of what's the matter.

It has probably something to do with synchronization and somesuch and I should probably have and declare Apuestas in some other way, but I'm quite new at this and I very happily would be enlightened.

I have also included this as an issue in the repo with the hacktoberbest label, just in case someone is interested in advancing one PR in that area.

Aucun commentaire:

Enregistrer un commentaire