Unit Testing a Custom Attribute Class

I have a custom attribute that is used only to indicate a member (no constructor , no properties ):

 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)] public sealed class MyCustomAttribute : Attribute { } 

How could I unit test this? And to clarify ... I know the "what", but not the "how"

I assume there is a unit test way to ensure the correct AttributeUsage in place? So how could I do this? Every time I create a mock class and try to add an attribute to the wrong thing, it will not let me compile, so how can I create a bad mock class for testing?

+6
source share
1 answer

You would not create a mock class to test this. Instead, you simply check the attribute class to see if it has AttributeUsageAttribute attribute properties. that's a sip

 [TestMethod] public void Is_Attribute_Multiple_False { var attributes = (IList<AttributeUsageAttribute>)typeof(MyCustomAttribute).GetCustomAttributes(typeof(AttributeUsageAttribute), false); Assert.AreEqual(1, attributes.Count); var attribute = attributes[0]; Assert.IsFalse(attribute.AllowMultiple); } //Etc. 
+10
source

Source: https://habr.com/ru/post/892607/


All Articles