NUnit:断言集合具有具有特定 属性 值的项目

NUnit: Assert collection has item with specific property value

我正在使用 NUnit 断言服务已正确添加到 ASP.Net 核心中的 IServiceCollection

我正在尝试确保服务集合有一个 ServiceDescriptor ServiceType 类型为 MyClass。我如何用 NUnit 语法重写这个断言?

IServiceCollection collection = ...;
Assert.NotNull(collection.SingleOrDefault(sd => sd.ServiceType == typeof(MyService)));

我假设它看起来像这样:

Assert.That(collection, Has.One.?????);

你想要

Assert.That(collection, Has.One.With.Property("ServiceType").EqualTo(typeof(MyService)));

"With" 是可选的,但似乎使它阅读起来更好。