2012-09-14 189 views
0

只見實踐TDD書爲測試授權控制,但我不明白到底是什麼意思,這是代碼測試授權控制器

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

回答

0

他們檢查了[Authorize]屬性已被添加到TodoController。請注意,他們實際上沒有測試授權機制是否工作,只是存在AuthorizeAttribute裝飾。

+1

如何實際測試,如果授權機制運作的? –

0

它檢查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 –