Pergunta

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. 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.

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.

// 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.

Could someone show me the proper way to extent Fluent Assertions to accomplish this?

Foi útil?

Solução

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.
  2. Fork the repository on GitHub and add it directly to the framework. I will accept pull requests.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top