Fluent Assertions PropertyInfo BeDecoratedWith
本文关键字:BeDecoratedWith PropertyInfo Assertions Fluent | 更新日期: 2023-09-27 18:30:40
.NET FluentAssertions库(版本2.1.0)具有多个BeDecoratedWith<T>()
实现,用于断言类型(或类型成员)应用了给定属性。这些调用如下所示:
typeof(X).Should()
.BeDecoratedWith<SomeAttribute>(attr => attr.Name == expectedValue);
lambda 表达式断言该属性的Name
等于某些expectedValue
。
当sut
是类型时,这很好,但是当它是成员时,没有采用 lambda 表达式的重载BeDecoratedWith<T>
。
// 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);
该文档快速涵盖了可扩展性,但我在弄清楚如何在接受上述 lambda 的 PropertyInfoAssertions
类上创建BeDecoratedWith<T>
的重载(或扩展方法)
有人可以告诉我扩展流利断言以实现这一目标的正确方法吗?
您有两个选择:
- 在 PropertyInfoAssertions 上创建一个扩展方法,该方法支持 lambda 表达式并使用 SubjectProperties 属性访问实际属性。
- 在 GitHub 上分叉存储库并将其直接添加到框架中。我将接受拉取请求。
一个可能的答案是等待一段时间,然后获取最新版本,因为这个问题似乎最近已修复:)
http://fluentassertions.codeplex.com/workitem/12455