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

问题描述 投票:0回答:1

我有一些使用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


    }

  }
}

我可以在这里得到帮助吗?

scala playframework scalatest functional-testing
1个回答
0
投票

几乎没有问题

(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.MapisEmpty()方法的任意对象
  • 具有Boolean isEmpty方法并返回parameterless的任意对象>
  • 其中“ 任意对象

”实际上是指任意Boolean对象reference
AnyRef

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}
© www.soinside.com 2019 - 2024. All rights reserved.