Scala 功能测试:如何进行否定断言?

Scala Functional tests: How to do negative assertion?

我有一些使用 Scala 和 play 框架 (2.4.*) 的 restful 服务,我正在尝试为这些服务编写一些功能测试,并在否定断言上遇到困难。例如:

如果我收到来自我的服务的响应(在 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 适用于以下类型

  • scala.collection.GenTraversable
  • String
  • Array
  • scala.Option
  • java.util.Collection
  • java.util.Map
  • 具有 isEmpty() 方法的任意对象 returns Boolean
  • 具有 parameterless isEmpty 方法的任意对象 returns Boolean

其中“任意对象”实际上是指任意 reference 对象 AnyRef

implicit def emptinessOfAnyRefWithIsEmptyMethod[T <: AnyRef { def isEmpty(): Boolean}]: Emptiness[T]

JsLookupResultnot AnyRef

的子类型
sealed trait JsLookupResult extends Any with JsReadable

因此,由于约束条件T <: AnyRef { def isEmpty(): Boolean}不满足,我们无法使用nice emptyness DSL。

或者以下应该有效

(jsonResponse \ "address").isEmpty mustBe false
(jsonResponse \ "address").isDefined mustBe true