Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVC自定义筛选器,手动调用ASP.NET管道事件进行单元测试_Asp.net_Asp.net Mvc 3_Unit Testing_Events_Protected - Fatal编程技术网

MVC自定义筛选器,手动调用ASP.NET管道事件进行单元测试

MVC自定义筛选器,手动调用ASP.NET管道事件进行单元测试,asp.net,asp.net-mvc-3,unit-testing,events,protected,Asp.net,Asp.net Mvc 3,Unit Testing,Events,Protected,我的所有控制器都继承自MyControllerBase。问题是,现在我无法对某些方法进行单元测试,因为过滤器设置了一些影响代码路径的授权/逻辑标志 是否有任何方法可以手动触发操作执行?管道如何触发这些事件 编辑:更多地展示此设计背后的想法,以回应评论。我基本上是这样的: public abstract class MyControllerBase : Controller { protected override void OnActionExecuting(ActionExecutin

我的所有控制器都继承自
MyControllerBase
。问题是,现在我无法对某些方法进行单元测试,因为过滤器设置了一些影响代码路径的授权/逻辑标志

是否有任何方法可以手动触发操作执行?管道如何触发这些事件

编辑:更多地展示此设计背后的想法,以回应评论。我基本上是这样的:

public abstract class MyControllerBase : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext context)
    {
        // do some magic
    }
}
因此,现在在
视图
控制器
中的任何位置,我都可以获得当前用户的详细信息,他们的电子邮件是什么,他们有什么授权,他们为哪个部门工作等等

例如:

public abstract class MyControllerBase : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext context)
    {
        UserProperties = 
             _userService
                .GetUserProperties(filterContext.HttpContext.User.Identity.Name);

        ViewBag.UserProperties = UserProperties;
    }

    public UserProperties { get; private set; }

    public bool CheckSomethingAboutUser()
    {
         return UserProperties != null 
            && UserProperties.IsAuthorisedToPerformThisAction;
    }

    // ... etc, other methods for querying UserProperties
}

因此,这种设计非常好,但是正如您所看到的,测试上述操作非常困难,因为我无法在测试设置中直接操作
UserProperties

我不确定您是否要像在MCV中那样重写
OnActionExecuting
,通常我会

然后你们班:

public class SomeMagicAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }
}
然后在单元测试中,您可以

[SomeMagic]
public abstract class MyControllerBase : Controller
{

}

这里的问题是,在
OnActionExecuting
事件中,我向
ViewBag
MyControllerBase
中的公共属性写入了一些标志。其思想是,这些值现在在所有控制器和视图中的任何位置都可用。我的记忆有点模糊,但我想我之所以选择我的设计,是因为我看不出如何使用自定义的
actionfilteratAttribute
。你有什么想法吗?你能把你的
OnActionExecuting
的一些神奇之处贴出来吗至少对于在viewbag中设置内容,您可以执行
filterContext.Controller.viewbag.magic=42我已经更新了问题,以显示设计背后的想法
OnActionExecuting
并没有做任何聪明的事情,它只是访问一些我希望全局可用的用户信息。如果你认为我做得不对,你的评论将不胜感激。不,你没有做得不对,事实上我刚刚检查了我正在进行的MVC项目,我们做得非常相似。-无论如何,从您的代码示例来看,似乎您并不真正需要
ActionExecutingContext
,因此您可以在基本构造函数中用
HttpContext.User.Identity.Name
填充UserProperties变量,对吧?不幸的是,这不起作用,因为
HttpContext
在控制器构造函数中为null。
[SomeMagic]
public abstract class MyControllerBase : Controller
{

}
var magic = new SomeMagicAttribute();
var simulatedContext = new ActionExecutingContext();
magic.OnActionExecuting(simulatedContext);