How to NUnit test for a method's attribute existence
public interface IMyServer
{
[OperationContract]
[DynamicResponseType]
[WebGet(UriTemplate = "info")]
string Se开发者_StackOverflow社区rverInfo();
}
How do I write an NUnit test to prove that the C# interface method has the [DynamicResponseType]
attribute set on it?
Something like:
Assert.IsTrue(Attribute.IsDefined(
typeof(IMyServer).GetMethod("ServerInfo"),
typeof(DynamicResponseTypeAttribute)));
You could also do something involving generics and delegates or expressions (instead of the string "ServerInfo"), but I'm not sure it is worth it.
For [WebGet]
:
WebGetAttribute attrib = (WebGetAttribute)Attribute.GetCustomAttribute(
typeof(IMyServer).GetMethod("ServerInfo"),
typeof(WebGetAttribute));
Assert.IsNotNull(attrib);
Assert.AreEqual("info", attrib.UriTemplate);
精彩评论