[英]Fluent Assertions ThatAreNotDecoratedWith
我想使用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或采用其他方法来解决没有特定属性的列出方法的根本问题,我将不胜感激。
目前,Fluent断言没有可让您这样做的公开成员。 最好的解决方案是转到GitHub上的Fluent Assertions项目,然后打开一个Issue或提交一个Pull Request,以在FA代码库中解决此问题。
我意识到这可能被视为无法回答,因此为了完整起见,我将抛出一个问题,即您可以使用反射来解决此问题,尽管通常适用于对私人成员进行反射的免责声明也适用。 这是一个可以使链接工作的实现方式:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.