我有那个 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/