FluentAssertions:如何指定集合应该包含一定数量的元素匹配谓词?

FluentAssertions: how to specify that collection should contain a certain number of elements matching predicate?

例如。我需要断言该列表:

var list = new List<string> { "James", "Michael", "Tom", "John" };

应包含一定数量(目前为 2 个)的元素匹配特定谓词:

list.Should().Contain(element => element.StartsWith("J"), 2);

但是这个方法没有这样的重载。 我怎样才能在 FluentAssertions 中做到这一点?

你不能。最接近的是将该行重写为

list.Where(element => element.StartsWith("J")).Should().HaveCount(2);