Thursday, May 15

ExpectedException on an inner exception

How do I do this? I have a test with an expected exception, but the act is going through reflection. The code throws the exception, but reflection is changing the exception. Can I use ExpectedException on an inner exception?
[TestMethod]
[ExpectedException(typeof(System.IndexOutOfRangeException))]
public void TestTransformationNegative()
{
   // Arrange 
   int[] numbers = { 10 };
   var target = new LinqProjectionAccessor(new LinqProjection());

   // Act
   string[] numbersAsText = target.Transformation(numbers);
}
Test Name: TestTransformationNegative Test FullName: UnitTestLinq.TestLinqProjection.TestTransformationNegative Test Source: c:\Users\tjmadden\Source\Workspaces\Linq\UnitTestLinq\TestLinqProjection.cs : line 128 Test Outcome: Failed Test Duration: 0:00:00.0264312 Result Message: Test method UnitTestLinq.TestLinqProjection.TestTransformationNegative threw exception System.Reflection.TargetInvocationException, but exception System.IndexOutOfRangeException was expected. Exception message: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. Result StackTrace: at Linq.LinqProjection.<>c__DisplayClass5.b__4(Int32 n) in c:\Users\tjmadden\Source\Workspaces\Linq\Linq\LinqProjection.cs:line 147 at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at Linq.LinqProjection.Transformation(Int32[] numbers) in c:\Users\tjmadden\Source\Workspaces\Linq\Linq\LinqProjection.cs:line 149 --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at UnitTestLinq.Accessors.AccessorReflection.RunMethod(Type objectType, String methodName, Object objectInstance, Object[] objectParameters) in c:\Users\tjmadden\Source\Workspaces\Linq\UnitTestLinq\Accessors\AccessorReflection.cs:line 51 at UnitTestLinq.Accessors.AccessorBase`1.RunMethod(String methodName, Object[] parameters) in c:\Users\tjmadden\Source\Workspaces\Linq\UnitTestLinq\Accessors\AccessorBase.cs:line 35 at UnitTestLinq.Accessors.LinqProjectionAccessor.Transformation(Int32[] numbers) in c:\Users\tjmadden\Source\Workspaces\Linq\UnitTestLinq\Accessors\LinqProjectionAccessors.cs:line 54 at UnitTestLinq.TestLinqProjection.TestTransformationNegative() in c:\Users\tjmadden\Source\Workspaces\Linq\UnitTestLinq\TestLinqProjection.cs:line 134

No comments: