Scalatest“;至少一个“;取而代之的是福尔

Scalatest“;至少一个“;取而代之的是福尔,scala,testing,scalatest,scalacheck,Scala,Testing,Scalatest,Scalacheck,我在Scala中编写此测试方法是为了测试REST服务 @Test def whenRequestProductInfo() { // When Request Product Info forAll { (productId: Int) => val result = mockMvc().perform(get(s"/products/$productId") .accept(MediaType.parseMediaType(APPLICATION_JS

我在Scala中编写此测试方法是为了测试REST服务

@Test def whenRequestProductInfo() {
  // When Request Product Info
  forAll { (productId: Int) =>
      val result = mockMvc().perform(get(s"/products/$productId")
        .accept(MediaType.parseMediaType(APPLICATION_JSON_CHARSET_UTF_8)))
        .andExpect(status.isOk)
        .andExpect(content.contentType(APPLICATION_JSON_CHARSET_UTF_8))
        .andReturn;

      val productInfo = mapper.readValue(result.getResponse.getContentAsString, classOf[ProductInfo])

      // At least one is not null
      // assert(productInfo.getInventoryInfo != null)
  }
}

但我想测试是否至少有一个productInfo.getInventoryInfo不为null,而不是每个productInfo.getInventoryInfo不为null,假设我们有一个产品ID列表:

val productIds: List[Int] = ???
我们应该考虑从
productId
productInfo
到另一个
val
的转换。(我认为这种方法或类似的东西会存在于其他地方的代码中)

现在我们有了库存信息列表,不管是什么类型的。我们至少可以使用
检查集合中的至少一个库存信息是否为
null

atLeast(1, inventoryInfo) should not be null

ScalaTest似乎没有任何类似于
forAll
的通用版本,因此如果需要进行大量计算,语法会有很大不同,也不是很好。

forAll可以通过配置,以获得所需的成功评估数量和允许的失败评估数量。这应该可以完成你想要的。在这一页的末尾

例如:

forAll (minSuccessful(1)) { (productId: Int) =>  ...

但您确实希望它适用于所有产品ID,对吗?即,对于所有产品标识,每个标识都有至少一个元素不为空的库存信息?
getInventoryInfo
是一个集合吗?不完全是。我想找到至少一个带有inventoryInfo的产品id=在本例中,nullforAll使用默认的Int生成器生成值(我认为)。我不确定这是你想要的。是否确实不想检查某个特定值?你看过《无论何时》吗?它可能会帮助你缩小你正在寻找的范围,并且仍然使用发电机。随时检查。
forAll (minSuccessful(1)) { (productId: Int) =>  ...