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

标签 .net asp.net-mvc unit-testing asp.net-mvc-3 asp.net-membership

我有那个 Controller :

[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。如何在我的 Controller 中测试角色和授权?

最佳答案

过滤器(例如,您的授权属性)被设计为独立于 Controller 和操作方法。当您为操作方法编写单元测试时,过滤器会被简单地忽略,它们只是属性。
但是,当您对 Controller 和 Action 进行单元测试时,您不必测试它们!
如果你想测试你的应用程序是如何一起工作的,你必须编写 ui 自动化测试。

更新:
Steven Sanderson 在他的书 pro asp.net mvc 2 framework 中有很好的解释。其实我重复了他上面的话

关于.net - 如何在 .net MVC 3 中使用角色测试授权属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6899916/

相关文章:

unit-testing - 如何使用最小起订量来验证企业库日志记录?

.net - 在 .NET 开发团队中创造最大速度的工具

jquery - 使用 Mocha 使用 Typescript 对 Jquery 进行单元测试时如何修复 "$ is not defined"错误?

c# - 处理带有动态列的表的最佳方法

asp.net-mvc - 使用同一个 ASP.NET MVC 应用托管多个域?

c# - 如何在 Razor mvc5 中每第 3 列创建一行?

c# - 使用 BindAttribute 的 Prefix 属性绑定(bind)到单个参数

c++ - 如何在boost中以编程方式获取测试结果

c# - 使用 C# 将应用程序发送到特定屏幕

c# - 从 WCF 客户端内部调试 NullReference 异常