我有一些使用Scala和play框架(2.4。*)的宁静服务,我正在尝试为这些服务编写一些功能测试,并在否定性断言上花了很多时间。例如:
如果我收到来自服务的回复(在Json中,如:]
{ "id":71324, "name":"Matt", "address":"24 Main st" }
我正在检查:
很难找到有关如何执行上述断言的示例。
对于其他断言,我是这样做的:
class IntegrationTest extends PlaySpec with OneServerPerSuite with MockitoSugar { // need with mockito*?
override lazy val app = new GuiceApplicationBuilder()
.configure(Configuration.apply(ConfigFactory.parseFile(new File("test/resources/testApplication.conf")).resolve()))
.overrides(bind[EmployeeDAO].to[MockEmployeeDAO])
.build
implicit lazy val log = LoggerFactory.getLogger(getClass)
val wsClient = app.injector.instanceOf[WSClient]
val myPublicAddress = s"localhost:$port"
"test1" must {
"get employee record" in {
val route = s"http://$myPublicAddress/INTERNAL/employee/7"
val response = await(wsClient.url(route).get())
log.debug(response.header("Content-Type") + " -> " + response.body)
val jsonResponse = Json.parse(response.body)
response.status mustBe OK
(jsonResponse \ "id").get mustBe JsNumber(71324)
(jsonResponse \ "name").get mustBe JsString("Matt")
//trying to check that there is no phone
//trying to check address fiels exists and is non-empty
//(jsonResponse \ "address").get mustNot empty -- got systax error
}
}
}
我可以在这里得到帮助吗?
几乎没有问题
(jsonResponse \ "address").get mustNot empty
(jsonResponse \ "address")
的类型是具有JsLookupResult
方法的isEmpty()
,因此尝试使用checking for emptiness似乎是合理的
(jsonResponse \ "address") mustNot be (empty)
但是,由于empty
DSL DSL适用于以下类型,所以这不起作用
empty
scala.collection.GenTraversable
String
Array
scala.Option
java.util.Collection
java.util.Map
的isEmpty()
方法的任意对象Boolean
isEmpty方法并返回parameterless
的任意对象>其中“ 任意对象
Boolean
对象referenceAnyRef
implicit def emptinessOfAnyRefWithIsEmptyMethod[T <: AnyRef { def isEmpty(): Boolean}]: Emptiness[T]
是JsLookupResult
的子类型not
AnyRef
因此,因为不满足约束
sealed trait JsLookupResult extends Any with JsReadable
,所以我们不能使用空的DSL。
或者以下方法应该起作用
T <: AnyRef { def isEmpty(): Boolean}