【发布时间】:2013-09-12 07:33:54
【问题描述】:
.NET FluentAssertions 库(版本 2.1.0)有几个 BeDecoratedWith<T>() 实现,用于断言类型(或类型成员)具有应用于它的给定属性。这些调用如下所示:
typeof(X).Should()
.BeDecoratedWith<SomeAttribute>(attr => attr.Name == expectedValue);
lambda 表达式断言该属性的 Name 等于某些 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,但我无法弄清楚如何在PropertyInfoAssertions 类上创建BeDecoratedWith<T> 的重载(或扩展方法),该类接受类似于上述的lambda。
有人可以告诉我扩展 Fluent Assertions 以实现此目的的正确方法吗?
【问题讨论】:
-
那么,您有什么特别的问题吗?
-
扩展库的最佳方式。看起来我可以扩展
ProprtyInfoAssertions,但是我必须自己重新编写所有属性处理代码,这似乎是错误的。 -
好的,现在我明白问题所在了。
标签: c# fluent-assertions