Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

unable to use configuration element with default values
blaz
#1 Posted : Tuesday, September 13, 2011 11:25:51 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 9/13/2011(UTC)
Posts: 3

It seems that something in the way NCrunch executes tests causes problems with reflection. Consequently I'm unable to set or access configuration properties with declared default value on configuration elements.

All tests in example below fail with following exception when executing in NCrunch. All tests execute successfully in NUnit runner.
If property DefaultValue is removed from configuration element property, all tests with exception of 'AccessPropertyWithDefaultValue' execute successfully in NCrunch and NUnit.

Can this problem be fixed with appropriate configuration?

Code:

System.Reflection.CustomAttributeFormatException : 'DefaultValue' property specified was not found.
  ----> System.Reflection.CustomAttributeFormatException : 'DefaultValue' property specified was not found.
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimePropertyInfo property, RuntimeType caType)
   at System.Reflection.RuntimePropertyInfo.GetCustomAttributes(Type attributeType, Boolean inherit)
   at System.Attribute.InternalGetCustomAttributes(PropertyInfo element, Type type, Boolean inherit)
   at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
   at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Configuration.ConfigurationElement.CreateConfigurationPropertyFromAttributes(PropertyInfo propertyInformation)
   at System.Configuration.ConfigurationElement.CreatePropertyBagFromType(Type type)
   at System.Configuration.ConfigurationElement.PropertiesFromType(Type type, ConfigurationPropertyCollection& result)
   at System.Configuration.ConfigurationElement.get_Properties()
   at System.Configuration.ConfigurationElement.get_Item(String propertyName)
   at NCrunchBugs.SampleConfigurationElement.get_First() in c:\Users\blorger\AppData\Local\NCrunch\4828\16\NCrunchBugs\DefaultConfigurationValuesBug.cs:line 47
   at NCrunchBugs.DefaultConfigurationValuesBug.AccessPropertyWithDefaultValue() in c:\Users\blorger\AppData\Local\NCrunch\4828\16\NCrunchBugs\DefaultConfigurationValuesBug.cs:line 28
--CustomAttributeFormatException
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)


Code:

using System.Configuration;
using NUnit.Framework;

namespace NCrunchBugs
{
    [TestFixture]
    public class DefaultConfigurationValuesBug

    {
        [Test]
        public static void SetPropertyWithDefaultValue()
        {
            var s = new SampleConfigurationElement();
            s.First = "dd";
        }

        [Test]
        public static void SetPropertyWithoutDefaultValue()
        {
            var s = new SampleConfigurationElement();
            s.Second = "dd";
        }

        [Test]
        public static void AccessPropertyWithDefaultValue()
        {
            var s = new SampleConfigurationElement();
            Assert.That(s.First, Is.Not.Empty);
        }

        [Test]
        public static void AccessPropertyWithoutDefaultValue()
        {
            var s = new SampleConfigurationElement();
            Assert.That(s.Second, Is.Empty);
        }
    }

    public class SampleConfigurationElement : ConfigurationElement
    {
        private const string FirstProperty = "first";
        private const string SecondProperty = "second";

        [ConfigurationProperty(FirstProperty, DefaultValue = "something")]
        public string First
        {
            get { return (string)this[FirstProperty]; }
            set { this[FirstProperty] = value; }
        }

        [ConfigurationProperty(SecondProperty)]
        public string Second
        {
            get { return (string)this[SecondProperty]; }
            set { this[SecondProperty] = value; }
        }
    }
}
Remco
#2 Posted : Tuesday, September 13, 2011 7:38:29 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Interesting ... do you still have this problem if you turn off instrumentation in the project file under test? (Note that turning off instrumentation will also stop NCrunch from finding tests in the built assembly, so you'll need to have your tests in a different project to try this).
blaz
#3 Posted : Thursday, September 15, 2011 6:08:12 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 9/13/2011(UTC)
Posts: 3

Thanks.

Turning off instrumentation solved the problem.
But I had to use attribute TestCase. Tests marked with attribute Test were not found when instrumentation was turned off. I guess this is another thing to fix for NUnit support.

I moved configuration element (SampleConfigurationElement) code to separate assembly. It is sufficient to disable instrumentation for this assembly. This is probably what you meant by "you'll need to have your tests in a different project".

Unfortunately this removes code coverage feedback for tested assembly. I hope original problem can be fixed.
Remco
#4 Posted : Thursday, September 15, 2011 12:34:58 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Thanks, this helps narrow things down a great deal. This result tells me that the problem is being caused by instrumentation removing some of the metadata that your tests are relying on. I'll see what I can do about implementing a fix. Sorry for the trouble.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.035 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download