测试授权控制器

问题描述:

只见实践TDD书为测试授权控制,但我不明白到底是什么意思,这是代码测试授权控制器

Assert.IsTrue(typeof (TodoController) 
        .GetCustomAttributes(true).ToList() 
        .Any(o=>o.GetType()==typeof(AuthorizeAttribute)) 
); 

他们检查了[Authorize]属性已被添加到TodoController。请注意,他们实际上没有测试授权机制是否工作,只是存在AuthorizeAttribute装饰。

+1

如何实际测试,如果授权机制运作的? –

它检查TodoController是否具有AuthorizeAttribute,即饰有[Authorize]

[Authorize] // <-- if this is present the test will pass, if not it will fail. 
public class TodoController { 
    // ... 
} 
+0

tnx.what GetCustomAttributes(true)正好 –

+0

它返回应用于该类型的所有自定义属性的数组。 agrgument应该设置为'true'来搜索这个成员的继承链来查找属性,否则就是'false'。参考:http://msdn.microsoft.com/en-us/library/kff8s254.aspx –