1

FluentAssertions を使用して、NonActionAttribute で装飾されていないすべてのメソッドをテストしたいと考えています。(これにより、T4MVC によってプレースホルダーとして自動的に生成される一連のアクション メソッドが削減されます。)

私の特定の問題は、MethodInfoSelector メソッドを連鎖させることです。私はこのようなものを書きたいと思います:

   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
    }

キャストが失敗するか、結果を IEnumberable に変換すると、追加の MethodInfoSelector メソッドをチェーンできません。

MethodInfoSelector を生成する方法、または特定の属性を持たないメソッドをリストするという根本的な問題に対する別のアプローチについて、助けていただければ幸いです。

4

1 に答える 1

1

現在、Fluent Assertions には、これを行うために公開されているメンバーはありません。最善の解決策は、GitHub の Fluent Assertions プロジェクトに移動し、Issue を開くか、プル リクエストを送信して、FA コード ベースでこれを修正することです。

これは非回答と見なされる可能性があることを認識しているため、完全を期すために、リフレクションを使用してこの問題を解決できることを除外しますが、プライベート メンバーのリフレクションに関する通常の免責事項が適用されます。以下は、チェーンを機能させる実装の 1 つです。

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;
}
于 2014-06-06T04:42:02.663 に答える