2010-01-05 45 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