2010-01-05 44 views
9
public interface IMyServer 
    { 
     [OperationContract] 
     [DynamicResponseType] 
     [WebGet(UriTemplate = "info")] 
     string ServerInfo(); 
    } 

如何編寫NUnit測試以證明C#接口方法對其設置了[DynamicResponseType]屬性?如何NUnit測試方法的屬性存在

+1

根據註釋 – 2010-01-05 18:18:54

回答

18

喜歡的東西:

Assert.IsTrue(Attribute.IsDefined(
      typeof(IMyServer).GetMethod("ServerInfo"), 
      typeof(DynamicResponseTypeAttribute))); 

你也可以做一些涉及仿製藥和委託或表達式(而不是字符串「ServerInfo」),但我不知道這是值得的。

對於[WebGet]

WebGetAttribute attrib = (WebGetAttribute)Attribute.GetCustomAttribute(
    typeof(IMyServer).GetMethod("ServerInfo"), 
    typeof(WebGetAttribute)); 
Assert.IsNotNull(attrib); 
Assert.AreEqual("info", attrib.UriTemplate); 
+1

完美的更新,謝謝。 還有一個項目...有沒有一種方法來測試[WebGet(UriTemplate =「info」)]屬性UriTemplate設置爲「info」? – 2010-01-05 16:59:02

+1

將更新爲顯示... – 2010-01-05 18:16:12

+0

我使用不同的語法來做同樣的事情。這個答案有點清潔,所以我要切換到那個。感謝發佈! – 2010-01-12 14:19:32