简体   繁体   中英

How to NUnit test for a method's attribute existence

   public interface IMyServer
    {
        [OperationContract]
        [DynamicResponseType]
        [WebGet(UriTemplate = "info")]
        string ServerInfo();
    }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM