1

.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 以实现此目的的正确方法吗?

4

2 回答 2

2

你有两个选择:

  1. 在支持 lambda 表达式并使用 SubjectProperties 属性访问实际属性的 PropertyInfoAssertions 上创建扩展方法。
  2. Fork GitHub 上的存储库并将其直接添加到框架中。我会接受拉取请求。
于 2013-09-09T06:27:28.377 回答
0

一个可能的答案是稍等片刻,然后获取最新版本,因为这个问题最近似乎已经修复 :)

http://fluentassertions.codeplex.com/workitem/12455

于 2013-09-08T20:57:41.597 回答