Asp.net mvc 测试授权控制器
我在TDD手册中看到了测试授权控制器的实践,但我不完全理解这是什么意思Asp.net mvc 测试授权控制器,asp.net-mvc,test-first,Asp.net Mvc,Test First,我在TDD手册中看到了测试授权控制器的实践,但我不完全理解这是什么意思 Assert.IsTrue(typeof (TodoController) .GetCustomAttributes(true).ToList() .Any(o=>o.GetType()==typeof(AuthorizeAttribute)) ); 他们正在检查[Authorize]属性是否已添加到TodoController。请注意,他们实际
Assert.IsTrue(typeof (TodoController)
.GetCustomAttributes(true).ToList()
.Any(o=>o.GetType()==typeof(AuthorizeAttribute))
);
他们正在检查
[Authorize]
属性是否已添加到TodoController
。请注意,他们实际上并没有测试授权机制是否工作,只是检查AuthorizeAttribute
装饰是否存在。它检查TodoController
是否具有AuthorizeAttribute
,即是否使用[authorized]
装饰
[Authorize] // <-- if this is present the test will pass, if not it will fail.
public class TodoController {
// ...
}
[Authorize]//tnx.GetCustomAttributes(true)是什么?它返回应用于该类型的所有自定义属性的数组。agrument应设置为true
,以搜索此成员的继承链以查找属性,否则设置为false
。参考: