chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
My job has me writing unit test for an existing library they have. I am using VS 2010 and when running the unit tests with Typemock disabled all the test pass. However, when I enable Typemock a few of the tests fail.
The method I am unit testing is used to validate method parameters. When the validation fails, it generates an exception and tries to reflect delegate to get the name of the parameter it was checking so it can include it in the name in the error message. However, when reflecting the delegate another ArgumentException is being generated by Typemock.

The error message I receive is the following:
"metadataToken Parameter name: Token 0x70000c1c is not a valid FieldInfo token in the scope of module Library.Test.dll"

Again, when Typemock is disabled this is not occurring. And since this existing code my employer is using is part of a larger project, we need to be able to run unit tests with Typemock enabled.

Any assistance or insight would be appreciated.

Thanks.

Here is my unit test

        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void AssertIsPositive_WithInt_ThrowsArgumentException_WhenArgumentIsZero()
        {
            int argument = 0;
            string expectedMessage = string.Format(CultureInfo.InvariantCulture, "{0} is not a positve number.
Parameter name: argument", argument);
            try
            {
                ArgumentValidator.AssertIsPositive(() => argument);
            }
            catch (ArgumentException aex)
            {
                Assert.AreEqual(expectedMessage, aex.Message);
                throw;
            }
        }


The method I am testing is the following.

      public static void AssertIsPositive(Func<int> argument)
      {
         if (argument() < 1 )
         {
            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "{0} is not a positve number.", argument()), ArgumentValidator.GetArgumentFieldName(argument));
         }
      }


The preceding method calls GetArgumentFieldName. This is where the ArgumentException is thrown when Typemock is enabled.
From the line that starts with "return argument.Target.GetType().Module.ResolveField".

      private static string GetArgumentFieldName<TValue>(Func<TValue> argument)
      {         
         try
         {
            int fieldHandle = BitConverter.ToInt32(argument.Method.GetMethodBody().GetILAsByteArray(), 2);
            return argument.Target.GetType().Module.ResolveField(fieldHandle, argument.Target.GetType().GetGenericArguments(), argument.Method.GetGenericArguments()).Name;
         }
         catch (ArgumentOutOfRangeException e)
         {
            throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The specified argument of type {0} is not a function containing a method parameter.", argument.GetType()), ExpressionHelper.GetMemberName(() => argument), e);
         }
      }
asked by amartinez (600 points)

1 Answer

0 votes
Hello,

I apologize for the late reply. Let me try and reproduce this locally, and I will contact you soon when I have more information.

Thank you very much for the sample code.
answered by igal (5.7k points)
...