简体   繁体   English

流利的断言PropertyInfo BeDecoratedWith

[英]Fluent Assertions PropertyInfo BeDecoratedWith

The .NET FluentAssertions library (version 2.1.0) has several BeDecoratedWith<T>() implementations for asserting that a type (or type member) has a given attribute applied to it. .NET FluentAssertions库(版本2.1.0)具有几个BeDecoratedWith<T>()实现,用于断言类型(或类型成员)具有给定的属性。 These calls look like this: 这些调用如下所示:

typeof(X).Should()
    .BeDecoratedWith<SomeAttribute>(attr => attr.Name == expectedValue);

The lambda expression asserts that the attribute has a Name equal to some expectedValue . lambda表达式断言该属性的Name等于某些expectedValue

This is great when the sut is a type, but when it is a member there is no overload of BeDecoratedWith<T> that takes a lambda expression. 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);

The documentation quickly covers extensibility , but I'm having trouble working out how I'd create an overload (or extension method) of BeDecoratedWith<T> on the PropertyInfoAssertions class that accepts a lambda like the one above. 该文档很快涵盖了可扩展性 ,但是我很难确定如何在接受像上面那样的lambda的PropertyInfoAssertions类上创建BeDecoratedWith<T>的重载(或扩展方法)。

Could someone show me the proper way to extent Fluent Assertions to accomplish this? 有人可以告诉我扩展Fluent Assertions的正确方法吗?

You have two options: 您有两种选择:

  1. Create an extension method on PropertyInfoAssertions that supports the lambda expression and uses the SubjectProperties property to access the actual property. 在PropertyInfoAssertions上创建支持lambda表达式并使用SubjectProperties属性访问实际属性的扩展方法。
  2. Fork the repository on GitHub and add it directly to the framework. GitHub上分叉存储库,并将其直接添加到框架中。 I will accept pull requests. 我将接受请求请求。

One possible answer is to wait a while and then get the latest version, as this issue seems to have been fixed recently :) 一个可能的答案是稍等片刻,然后获取最新版本,因为该问题似乎最近已得到解决:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM