如何在.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;
}
...
}
我正在使用 RoleProvider。如何测试控制器中的角色和授权?
I'm have that 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;
}
...
}
I'm using RoleProvider. How I can testing Roles and Authorization in my Controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
过滤器(例如,您的授权属性)被设计为独立于控制器和操作方法。当您为操作方法编写单元测试时,过滤器将被简单地忽略,它们只是属性。
但是,当您对控制器和操作进行单元测试时,您不必测试它们!
如果您想测试您的应用程序如何一起工作,您必须编写 ui 自动化测试。
更新:
Steven Sanderson 在他的《pro asp.net mvc 2 框架》一书中对此有很好的解释。其实我重复了上面他的话
Filters (for example, your authorizeattribute) are designed to be independent of the controllers and action methods. when you are writing unit tests for you action methods, filters are simply ignored, they are just attributes.
But, you do not have to test them when you are unit testing your controllers and actions!
If you want to test how your app works alltogether, you must write ui automation tests.
Update:
Steven Sanderson have great explanation of that in his book pro asp.net mvc 2 framework. In fact, I repeated his words above
我相信这是再次进行集成测试。 Watin ( http://watin.org/ ) 是一个用于 Web 集成测试的框架。
I believe this is for integration test again. Watin ( http://watin.org/ ) is a framework for Web integration test.