Question

I have a small Play (2.1.2) application that tries to store some data and perform a redirect. I have 2 specs:

"save the user" in {
  running(FakeApplication()) {
    val Some(create) = route(
      FakeRequest(PUT, "/users")
        .withSession(("user-id", user_id))
        .withFormUrlEncodedBody(("username", any_username))
    )

    status(create) must equalTo(SEE_OTHER)
    redirectLocation(create).map(_ must equalTo("/profile")) getOrElse failure("missing redirect location")
  }
}

"display errors with missing username" in {
  running(FakeApplication()) {
    val Some(create) = route(
      FakeRequest(PUT, "/users")
        .withSession(("user-id", user_id))
    )

    status(create) must equalTo(BAD_REQUEST)
    contentAsString(create) must contain ("This field is required")
  }
}

When I run these tests, the second test has the same result as the first one, so a SEE_OTHER instead of the BAD_REQUEST. When I change the order of the tests, both work fine. The second one also passes when I delete the first one.

Does Scala / Play / Specs2 somehow remember state across tests or requests? Is there anything I need to do to ensure they run in isolation?

EDIT:

The code in my controller looks like this:

val form: Form[User] = Form(
  mapping(
    "username" -> nonEmptyText
  )(user => User(username))(user=> Some(user.username))
)

form.bindFromRequest.fold(
  errors => BadRequest(views.html.signup(errors)),
  user => Redirect("/profile")
)
Was it helpful?

Solution

Playframework 2/Specs2 does not keep state between tests unless you keep state in your test classes, your application or any external place you keep data.

If for example your application would save a user to a database in one test and test for the existance of that user in another test then of course that would make your test interfere with each other.

So I guess you need to figure out some way to clean out the database where you keep your data between each test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top