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

Aca*_*uza 4 .net asp.net-mvc unit-testing asp.net-membership asp.net-mvc-3

我有那个控制器:

[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;
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

我正在使用RoleProvider.我如何在控制器中测试角色和授权?

obj*_*box 7

过滤器(例如,您的authorizeattribute)旨在独立于控制器和操作方法.当你为你的动作方法编写单元测试时,简单地忽略过滤器,它们只是属性.但是,当您对控制器和操作进行单元测试时,您不必测试它们!如果您想测试您的应用程序的工作方式,您必须编写ui自动化测试.

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