.NET FluentAssertions库(2.1.0 版)有几个BeDecoratedWith<T>()
实现,用于断言类型(或类型成员)具有应用于它的给定属性。这些调用如下所示:
typeof(X).Should()
.BeDecoratedWith<SomeAttribute>(attr => attr.Name == expectedValue);
lambda 表达式断言该属性具有Name
等于 some expectedValue
。
当它是一个类型时这很好sut
,但是当它是一个成员时,它没有重载BeDecoratedWith<T>
需要一个 lambda 表达式。
// compiler error: Cannot convert lambda expression to type 'string' because it is not a delegate type
typeof(X).GetProperty("xyz").Should()
.BeDecoratedWith<SomeAttribute>(attr => attr.Name == expectedValue);
该文档很快涵盖了extensibility,但我无法弄清楚如何在接受上述 lambdaBeDecoratedWith<T>
的PropertyInfoAssertions
类上创建重载(或扩展方法)。
有人可以向我展示扩展 Fluent Assertions 以实现此目的的正确方法吗?