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

Notification

Icon
Error

Unable to cast object of type 'System.Int32' to type 'System.String'
southernsun
#1 Posted : Thursday, April 2, 2015 1:25:41 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/2/2015(UTC)
Posts: 2
Location: Netherlands

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
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

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

Code:
        [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

Code:
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!

Code:
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)
Remco
#2 Posted : Thursday, April 2, 2015 10:27:59 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,986

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
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.
1 user thanked Remco for this useful post.
southernsun on 4/3/2015(UTC)
southernsun
#3 Posted : Friday, April 3, 2015 8:12:02 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/2/2015(UTC)
Posts: 2
Location: Netherlands

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Thank you, that works
1 user thanked southernsun for this useful post.
Remco on 4/3/2015(UTC)
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.045 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download