我有一个小型 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")
}
}
当我运行这些测试时,第二个测试的结果与第一个相同,所以 aSEE_OTHER
而不是BAD_REQUEST
. 当我更改测试顺序时,两者都可以正常工作。当我删除第一个时,第二个也通过了。
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")
)