我的服务路线:
get(
path("add" / IntNumber / IntNumber)( (a, b) =>
complete((a + b).toString())
)
) ~
post(
path("add") (
formFields('a.as[Int], 'b.as[Int]) {
(a, b) => complete((a + b).toString())
})
)
我的规格:
import spray.http.FormData
class RouteDefinitionSpec
extends org.specs2.mutable.Specification
with org.specs2.ScalaCheck
with spray.testkit.Specs2RouteTest with RouteDefinition {
def actorRefFactory = system
"the route" should {
"add with get requests" in {
prop { (a: Int, b: Int) =>
Get(s"/add/$a/$b") ~> route ~> check {
responseAs[String] === s"${a+b}"
}
}
}
"add with post form data request" in {
prop { (a: Int, b: Int) =>
Post("/add", FormData(Seq("a" -> a.toString, "b" -> b.toString))) ~> route ~> check {
responseAs[String] === s"${a+b}"
}
}
}
}
}
如果在浏览器中测试,GET 和 POST 路由都可以正常工作。POST 也可以在测试中使用。我的 GET 路线有什么问题?为什么不能测试?是什么导致了这样的错误以及如何避免它?
[info] RouteDefinitionSpec
[info]
[info] the route should
[error] x add with get requests
[error] Falsified after 0 passed tests.
[error] > ARG_0: 2147483647
[error] > ARG_1: -2147483648
[error] > Request was not handled (RouteDefinitionSpec.scala:5)
[info]
[info] + add with post form data request
[info]
[info]
[info] Total for specification RouteDefinitionSpec
[info] Finished in 393 ms
[info] 2 examples, 102 expectations, 1 failure, 0 error
更新: 这似乎与 scalacheck 有关,因为以下非基于属性的测试也是“绿色”的:
"add test without scalacheck" in {
Get("/add/30/58") ~> route ~> check {
responseAs[String] === "88"
}
}