2011-07-12 175 views
6

我有一個只是用來標記的成員(不constructor,沒有properties)一custom attribute單元測試自定義屬性

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)] 
public sealed class MyCustomAttribute : Attribute { } 

如何將我的單元測試嗎?並澄清......我知道'什麼',但不是'如何'我假設有一種方法來單元測試它以確保適當的AttributeUsage是適當的?那麼我怎麼能做到這一點?每次我創建一個模擬類,並嘗試將該屬性添加到錯誤的東西,它不會讓我編譯,所以我如何創建一個壞的模擬類來測試?

+0

@Kornelije:我定他的頭銜給他。如果你點擊他的名字,他一定會把他最後一個問題的標題留在那裏。 :) –

+0

是的,對不起。 Firefox在我的參賽表格中已經有了同樣的問題和標題。我忘了更改標題。 – michael

+1

@michael,您需要100%的代碼覆蓋率? :-) –

回答

10

你不會創建一個模擬類來測試這個。相反,您只需測試屬性類本身以查看它是否具有適當的AttributeUsageAttribute屬性屬性。 噢,什麼是一口

[TestMethod] 
public void Is_Attribute_Multiple_False 
{ 
    var attributes = (IList<AttributeUsageAttribute>)typeof(MyCustomAttribute).GetCustomAttributes(typeof(AttributeUsageAttribute), false); 
    Assert.AreEqual(1, attributes.Count); 

    var attribute = attributes[0]; 
    Assert.IsFalse(attribute.AllowMultiple); 
} 

//Etc.