我在Scala中玩过2.2并且对该框架有些新意。我似乎无法弄清楚如何进行请求测试,我可以找出集成测试,但不是请求测试。如果有人能指出我的方向非常有帮助。
我看了这个,但我不想测试控制器,模型或进行集成测试。我只想发布到路线并验证它返回我期望的。 http://www.playframework.com/documentation/2.2.x/ScalaTest
答案 0 :(得分:1)
来自http://www.playframework.com/documentation/2.2.x/ScalaFunctionalTest:
"respond to the index Action" in new WithApplication() {
val Some(result) = route(FakeRequest(GET, "/Bob"))
status(result) must equalTo(OK)
contentType(result) must beSome("text/html")
charset(result) must beSome("utf-8")
contentAsString(result) must contain("Hello Bob")
}
答案 1 :(得分:0)
这就是我弄清楚的方法。我不确定它是否是最佳方式,但它符合我的目标。
import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.test._
import play.api.test.Helpers._
import play.api.libs.json._
@RunWith(classOf[JUnitRunner])
class UsersSpec extends Specification {
"Users" should {
"render index request" in new WithApplication{
val response = route(FakeRequest(GET, "/users")).get
val result = Map("next" -> "hello")
contentAsString(response) must contain (Json.toJson(result).toString)
contentType(response) must beSome.which(_ == "application/json")
}
}