Sunday, 8 September 2013

Fluent Assersions PropertyInfo BeDecoratedWith

Fluent Assersions PropertyInfo BeDecoratedWith

The .NET FluentAssertions library 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?

No comments:

Post a Comment