1

I would like to use FluentAssertions to test for all methods that are not decorated with the NonActionAttribute. (This will reduce the set of action methods automatically generated as placeholders by T4MVC.)

My specific problem is with chaining together MethodInfoSelector methods. I would like to write something like this:

   public MethodInfoSelector AllActionMethods() {
        return TestControllerType.Methods()
            .ThatReturn<ActionResult>()
            .ThatAreNotDecoratedWith<NonActionAttribute>();
   }

    public static MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>(this IEnumerable<MethodInfo> selectedMethods) {
        return (MethodInfoSelector)(selectedMethods.Where(method => !method.GetCustomAttributes(false).OfType<TAttribute>().Any())); // this cast fails
    }

Either the cast fails, or if I convert my results to IEnumberable I can't chain additional MethodInfoSelector methods.

I would appreciate any help with either how to generate a MethodInfoSelector or a different approach to the underlying problem of listing methods that do not have a specific attribute.

Kevin Swarts
  • 435
  • 7
  • 17

1 Answers1

1

Fluent Assertions currently has no publicly exposed members to allow you to do this. Your best solution is to go over to the Fluent Assertions project on GitHub and either open an Issue or submit a Pull Request in order to get this fixed in the FA code base.

I realize this might be viewed as a non-answer, so for completeness I will throw out that you could solve this problem using reflection, though the usual disclaimers about reflecting on private members apply. Here is one such implementation that will let chaining work:

public static MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>( this MethodInfoSelector selectedMethods)
{
    IEnumerable<MethodInfo> methodsNotDecorated = selectedMethods.Where(
        method =>
            !method.GetCustomAttributes(false)
                .OfType<TAttribute>()
                .Any());

    FieldInfo selectedMethodsField =
        typeof (MethodInfoSelector).GetField("selectedMethods",
            BindingFlags.Instance | BindingFlags.NonPublic);

    selectedMethodsField.SetValue(selectedMethods, methodsNotDecorated);

    return selectedMethods;
}
vossad01
  • 11,552
  • 8
  • 56
  • 109
  • Thanks @vossad, I tried the code and it compiles, but in at least one place I use the results as NUnit test cases, and that causes NUnit to crash. I think your first answer was the best one. – Kevin Swarts Jun 06 '14 at 13:14
  • I agree, we didn't anticipate for this need yet. Either an issue or a Pull Request will get this solved. – Dennis Doomen Jun 06 '14 at 14:03