Build/Test Issues

Unable to cast object of type 'System.Int32' to type 'System.String'

Started by southernsun on 6,475 views

I'm getting the following failure on my tests projects when using nCrunch. My bet is that it is related MyTestCategoryAttribute. We have extended the TestCategoryBaseAttribute with a custom TestCategoryAttribute that takes enum values. Let's not discuss why we are using that, but that's just the way it is. Anyway, i think that is causing issues when nCrunch, for some reason, wants to analyze the attribute. Normally the TestCategoryAttribute takes a list of strings as parameters, but now it takes a list of enum values (which are represented as Ints), which causes this cast to fail?!

Here is an example of the code

public class MyTestCategoryAttribute : TestCategoryBaseAttribute
    {
        private List<string> testcategories = new List<string>();

        public MyTestCategoryAttribute(params TestCategory[] categories)
        {
            foreach (var category in categories)
            {
                this.testcategories.Add(category.ToString());
            }
        }

        public override IList<string> TestCategories
        {
            get { return this.testcategories; }
        }
    }


Which is used like this

        [TestMethod]
        [Description("Validates the attributes on the test methods.")]
        [MyTestCategory(TestCategory.Something, TestCategory.ContinuousIntegration)]
        public void ValidateAttributes()
        {
            ValidateTestAttributesTest.TestValidateTestAttributes(this.TestContext);
        }


Do you agree, or could it be something else that is causing this? Should we wait for a new release of ncrunch of is there a workaround?

EDIT: After looking at the nCrunch code using Reflector i'm pretty sure that this above mentioned code is causing the problem. I think nCrunch.Module.MSTest.Integration.MSTestFramework.getCategoryValue(ReflectedAttribute attribute) is hitting the last return statement

return new string[] { ((string) attribute2.ConstructorParameters[0].Value) };


and will fail because the parameter is an Enumerable/Int and not a string. Beter would be to call ToString() on .Value


Thanks!

NCrunch: If you are experiencing problems in getting this project to build, have a look at http://www.ncrunch.net/documentation/troubleshooting_project-build-issues
System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
   at nCrunch.Module.MSTest.Integration.MSTestFramework.getCategoryValue(ReflectedAttribute attribute)
   at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at nCrunch.Reflection.ReflectedMember.GetValuesFromNamedAttributes[T](String[] attributeTypeNames, Func`2 getValueFunc)
   at nCrunch.Module.MSTest.Integration.MSTestFramework.resolveCategories(ReflectedMethod method)
   at nCrunch.Module.MSTest.Integration.MSTestFramework.FindFrameworkTestsInAssembly(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, ComponentUniqueName testComponentUniqueName)
   at nCrunch.TestExecution.TestFinder.FindTestsForFrameworks(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, TestFrameworkDescription[] frameworks, ComponentUniqueName testComponentUniqueName)
   at nCrunch.Compiler.StaticManipulation.BuiltAssembly.DiscoverTests(TestFrameworkDescription[] applicableTestFrameworks, ComponentUniqueName testComponentUniqueName)
   at nCrunch.Compiler.RemoteBuildRunner.(ComponentBuildParameters , FilePath , BuildOutput , DirectoryPath[] , FilePath[] )
   at nCrunch.Compiler.RemoteBuildRunner.Build(ComponentBuildParameters parameters)

Edited

Hi, thanks for sharing this.

By default, NCrunch uses assembly metadata to discover tests using MSTest. This means that it isn't actually able to execute code within the assembly (i.e. it can't run the code inside your attribute). To compensate, it scrapes the constructor parameters from Category attributes and uses these to establish test categories. This naturally results in an exception when it encounters your custom category attribute.

If you change the 'Framework utilisation type for MSTest' solution-level NCrunch configuration setting to 'DynamicAnalysis', NCrunch will use a run-time environment to discover the tests and your attribute should work as designed.

You can read more about framework utilisation types here - http://www.ncrunch.net/documentation/reference_solution-configuration_framework-utilisation-types[http://www.ncrunch.net/documentation/reference_solution-configuration_framework-utilisation-types.

Post a reply

Log in to reply.