简体   繁体   English

断言该列表包含具有任何顺序的特定属性的元素

[英]Assert that list contains elements with specific properties in any order

In Junit5 i used the following snippet often to test if a collection contains elements that fullfill certain criteria:在 Junit5 中,我经常使用以下代码片段来测试集合是否包含满足某些条件的元素:

assertThat("The list of addresses", addresses.getAddressReferences(), containsInAnyOrder(
        allOf(
            hasProperty("id", is("abc")),
            hasProperty("role", is(SENDER))
        ),
        allOf(
            hasProperty("id", is("def")),
            hasProperty("role", is(RECEIVER))
        )
    ));

I did not yet find a way to express this in kotest I tried shouldContainExactlyInAnyOrder and such but they expect an actual object.我还没有找到在 kotest 中表达这一点的方法,我尝试shouldContainExactlyInAnyOrder等,但他们期望实际的 object。 I don't know how to express that i want to test each element against a different matcher.我不知道如何表达我想针对不同的匹配器测试每个元素。

I also checked the inspectors which might be the solution but they only seem to help to make assertions on ALL elements in the list at once.我还检查了可能是解决方案的检查员,但他们似乎只有助于一次对列表中的所有元素做出断言。

Kotest Inspectors might be what you are looking for. Kotest Inspectors可能是您正在寻找的。 They allow to test that all, none, or a specific number of elements in a collection satisfy specific assertions.它们允许测试集合中的所有、无或特定数量的元素是否满足特定断言。

Without knowledge of the address references data types I would assume that your example can be translated to Kotest like this:在不了解地址引用数据类型的情况下,我会假设您的示例可以像这样转换为 Kotest:

withClue("The list of addresses") {
    val references = addresses.getAddressReferences()
    assertSoftly {
        references shouldHaveSize 2
        references.forOne {
            it.id shouldBe "abc"
            it.role shouldBe SENDER
        }
        references.forOne {
            it.id shouldBe "def"
            it.role shouldBe RECEIVER
        }
    }
}

As pointed out by @LeoColman, the withClue and assertSoftly are not mandatory:正如@LeoColman 所指出的, withClueassertSoftly不是强制性的:

  • withClue just adds the given clue as additional info to the assertion error message in case an assertion fails. withClue只是将给定的线索作为附加信息添加到断言错误消息中,以防断言失败。
  • assertSoftly lets all of the assertions inside it run instead of stopping at the first failed assertion. assertSoftly内部的所有断言都运行,而不是在第一个失败的断言处停止。 This means that if we have for instance a collection of 3 elements, we still get information about the fact that there is or is not an element with id "abc" and role SENDER .这意味着,例如,如果我们有 3 个元素的集合,我们仍然可以获得关于存在或不存在id"abc"role SENDER的元素的信息。

Using inspectors as suggested by @Karsten seems like the way here!按照@Karsten 的建议使用检查员似乎就是这里的方式!

I present a simpler way to make the same assertions if you want to keep the code smaller:如果你想保持代码更小,我提出了一种更简单的方法来做出相同的断言:

val references = addresses.getAddressReferences()
references.forOne {
    it.id shouldBe "abc"
    it.role shouldBe SENDER
}

references.forOne {
    it.id shouldBe "def"
    it.role shouldBe RECEIVER
}

Both withClue and assertSoftly are optional withClueassertSoftly都是可选的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM