如何做一个“ allnotsatatify”富有量的条件

发布于 2025-02-07 10:37:59 字数 745 浏览 1 评论 0原文

考虑以下代码:

var ints = new List<Dictionary<string, string>>()
{
   new Dictionary<string, string>() { { "1", "bill" }, { "2", "john" } },
   new Dictionary<string, string>() { { "2", "jane" }, { "3", "alex" } }
};

这有效:

ints.Should().AllSatisfy(x => x.ContainsKey("2"));

但是,我想写一个断言,断言词典都没有一个“ 4”作为钥匙...

最初我认为我可以做到这一点:

ints.Should().AllSatisfy(x => !x.ContainsKey("2"));

但是那是不起作用的...我获取仅分配,呼叫,增量,减少,等待表达和新对象表达式可以用作语句

有没有任何方法可以在fluentAssertions中进行愚蠢地进行此操作?

我知道我可以做:

ints.Where(x => x.ContainsKey("2")).Should().BeEmpty();

对于为什么流利的断言可以将动作作为这种情况,我有些困惑。

Consider this code:

var ints = new List<Dictionary<string, string>>()
{
   new Dictionary<string, string>() { { "1", "bill" }, { "2", "john" } },
   new Dictionary<string, string>() { { "2", "jane" }, { "3", "alex" } }
};

This works:

ints.Should().AllSatisfy(x => x.ContainsKey("2"));

However, I want to write an assertions that asserts that none of the dictionaries contains a "4" as key...

Initially I thought I could do this:

ints.Should().AllSatisfy(x => !x.ContainsKey("2"));

But that doesn't work... I get Only assignment, call, increment, decrement, await expression, and new object expressions can be used as a statement

Is there any way to do this idiomatically in FluentAssertions?

I know that I can do:

ints.Where(x => x.ContainsKey("2")).Should().BeEmpty();

I'm a little stumped as to why fluent assertions can use actions as conditions like this where the return type is ignored.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2025-02-14 10:37:59

您可以使用:

ints.Should().OnlyContain(x => !x.ContainsKey("2"));

You can use:

ints.Should().OnlyContain(x => !x.ContainsKey("2"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文