Scalatra 测试全解析
1. 响应断言
状态码只是响应的一部分,接下来我们将增强测试,以覆盖响应的所有基本部分。一个 HTTP 响应主要由三部分组成:
- 状态
- 头部
- 主体
以下是一个测试示例:
class FoodServletSpec extends ScalatraSpec {
def is = s2"""
GET /foods/potatoes on FoodServlet
should return status 200
$potatoesOk
should be JSON
$potatoesJson
should contain name potatoes
$potatoesName
"""
addServlet(classOf[FoodServlet], "/*")
def potatoesOk = get("/foods/potatoes") {
status must_== 200
}
def potatoesJson = get("/foods/potatoes") {
header("Content-Type") must startWith ("application/json;")
}
def potatoesName = get("/foods/potatoes") {
body must contain("""{name: "potatos"}""")
}
}
这里使用了 Specs2 的匹配器,如
超级会员免费看
订阅专栏 解锁全文

241

被折叠的 条评论
为什么被折叠?



