[2.1.3 - scala] running multiple tests through a spec using test http server.

138 views
Skip to first unread message

Daniel Adams

unread,
Sep 5, 2013, 2:44:50 PM9/5/13
to play-fr...@googlegroups.com
I have an endpoint on my play project that I wish to test using a test http server with a fake application.  This endpoint points to a controller which feeds requests into an akka actor system which, using tells to other actors, will validate the request and if there is a validation error generate the appropriate db record with the details.

I created a specification to test this endpoint with a collection of requests that should cover the different edge cases.  for each element in the collection I want to send the request to the endpoint and check the database for the expected results.

The problem that I am having is that after the first pass, the first actor the controller calls is terminated and the second request goes to dead letters.  I've attempted to do a nested example where the outer example loads the test server and application and inside I build a group of examples with a foreach, but this led to more problems.  Does anyone have any idea why creating multiple test servers with fake applications would lead to the actors being terminated ( or not being initialized on the second application )?

Here is the portion of the spec I'm having issues with.

//Spec2
import org.specs2.mutable._

//Play
import play.api.test._
import play.api.test.Helpers._
import play.api.libs.ws._

class MessageSpec extends Specification {
  val specs: Traversable[Seq[String]] = csv(resource("replication.csv")) //using fireotter to parse the data from a csv file

  "The Message Request" should {
    //iterate over each spec
    specs foreach { spec =>
      val test = spec(0).toString
      val name = spec(1).toString
      val message = spec(2).toString
      val date = spec(3).toString

      test in new WithServer(app = fakeApp) {
       
        //build and send the request to the messages endpoint
        //message will be received by controller and passed on to actor system to be analyzed.
       
        await(WS.url("http://localhost:19001/messages").post(Map[String, Seq[String]](
          "name" -> Seq(name),
          "message" -> Seq(message),
          "date" -> Seq(date))))

        Thread.sleep(100)  //waiting for actors to complete analysis of the request

        //Open app.DB Connection and compare actual results to expected results.  ie, actual must equalTo(expected)
        
      }
    }
  }
}


Daniel Adams

unread,
Sep 6, 2013, 10:03:57 AM9/6/13
to play-fr...@googlegroups.com
For those that are interested, the issue with the code was not what I expected.  I was able to get the requests to run in a single app, by changing the code to this syntax

"The Message Request" should {
    "run in new server" >> {
      running(TestServer(19001, fakeApp)) {
        specs foreach { spec =>
                    test >> {
            1 must equalTo(1)
          }
        }
      }
    }
  }

When i first tried this, I was trying with new WebServer(app = fakeApp), as is written in the play documentation on running a test in a web server..  

  "The Message Request" should {
    "run in new server" >> new WithServer(app = fakeApp) {
      specs foreach { spec =>
        test >> {
          1 must equalTo(1)
        }
      }
    }
  }


But doing it that way resulted in the inner tests not being examined.  The output was that the outer test passed.  I'm really not sure what the difference is between the two methods, as they both seem to be wrapping the foreach tests in a test server with fake application and I haven't been able to find much documentation on the differences between running() and WithServer
Reply all
Reply to author
Forward
0 new messages