views:

68

answers:

1
   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?

+4  A: 

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);
Marc Gravell
Perfect, thank you. One more item... is there a way to test that the [WebGet(UriTemplate = "info")] attributes UriTemplate is set to "info"?
John E
Will update to show...
Marc Gravell
I was using different syntax to do the same thing. This answer is a little cleaner so I'm going to switch to that. Thanks for posting!
Peter Bernier