一部のデータを保存してリダイレクトを実行しようとする小さな Play (2.1.2) アプリケーションがあります。私は2つの仕様を持っています:
"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")
}
}
これらのテストを実行すると、2 番目のテストは最初のテストと同じ結果になるためSEE_OTHER
、BAD_REQUEST
. テストの順序を変更すると、両方とも正常に動作します。最初のものを削除すると、2番目のものも通過します。
Scala / Play / Specs2 は何らかの形でテストやリクエストの状態を記憶していますか? 確実に分離して実行するために必要なことはありますか?
編集:
私のコントローラーのコードは次のようになります。
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")
)