Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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
如何在.net MVC 3中使用角色测试授权属性?_.net_Asp.net Mvc_Unit Testing_Asp.net Mvc 3_Asp.net Membership - Fatal编程技术网

如何在.net MVC 3中使用角色测试授权属性?

如何在.net MVC 3中使用角色测试授权属性?,.net,asp.net-mvc,unit-testing,asp.net-mvc-3,asp.net-membership,.net,Asp.net Mvc,Unit Testing,Asp.net Mvc 3,Asp.net Membership,我很高兴有那个控制器: [Authorize(Roles = "Administrator")] public class ApuradorController : Controller { private readonly Questiona2011Context _context; private readonly AuthenticationService _authenticationService; public ApuradorController(Ques

我很高兴有那个控制器:

[Authorize(Roles = "Administrator")]
public class ApuradorController : Controller
{
    private readonly Questiona2011Context _context;
    private readonly AuthenticationService _authenticationService;

    public ApuradorController(Questiona2011Context context, AuthenticationService authenticationService)
    {
        this._context = context;
        this._authenticationService = authenticationService;
    }

    ...
}
我用的是RoleProvider。如何在控制器中测试角色和授权?

过滤器(例如,您的authorizeattribute)设计为独立于控制器和操作方法。当您为动作方法编写单元测试时,过滤器被忽略,它们只是属性。 但是,在单元测试控制器和操作时,不必测试它们! 如果您想测试应用程序如何协同工作,则必须编写ui自动化测试

更新:
Steven Sanderson在他的书pro asp.net mvc 2框架中对此有很好的解释。事实上,我重复了他上面的话

我相信这是为了再次进行集成测试。Watin()是一个Web集成测试框架。

根据过滤器的实现方式,您应该能够直接为它们编写测试。然后,在控制器的单元测试中,只需测试它们的行为,因为正如objectbox所指出的,它们将被忽略。