예제 #1
0
        /// <summary>
        /// Resolves the expected exception attribute. The function will try to
        /// get all the expected exception attributes defined for a testMethod.
        /// </summary>
        /// <param name="methodInfo">The MethodInfo instance.</param>
        /// <param name="testMethod">The test method.</param>
        /// <returns>
        /// The expected exception attribute found for this test. Null if not found.
        /// </returns>
        public virtual ExpectedExceptionBaseAttribute ResolveExpectedExceptionHelper(MethodInfo methodInfo, TestMethod testMethod)
        {
            Debug.Assert(methodInfo != null, "MethodInfo should be non-null");

            // Get the expected exception attribute
            ExpectedExceptionBaseAttribute[] expectedExceptions;
            try
            {
                expectedExceptions = GetCustomAttributes(methodInfo, typeof(ExpectedExceptionBaseAttribute), true).OfType <ExpectedExceptionBaseAttribute>().ToArray();
            }
            catch (Exception ex)
            {
                // If construction of the attribute throws an exception, indicate that there was an
                // error when trying to run the test
                string errorMessage = string.Format(
                    CultureInfo.CurrentCulture,
                    Resource.UTA_ExpectedExceptionAttributeConstructionException,
                    testMethod.FullClassName,
                    testMethod.Name,
                    StackTraceHelper.GetExceptionMessage(ex));
                throw new TypeInspectionException(errorMessage);
            }

            if (expectedExceptions == null || expectedExceptions.Length == 0)
            {
                return(null);
            }

            // Verify that there is only one attribute (multiple attributes derived from
            // ExpectedExceptionBaseAttribute are not allowed on a test method)
            if (expectedExceptions.Length > 1)
            {
                string errorMessage = string.Format(
                    CultureInfo.CurrentCulture,
                    Resource.UTA_MultipleExpectedExceptionsOnTestMethod,
                    testMethod.FullClassName,
                    testMethod.Name);
                throw new TypeInspectionException(errorMessage);
            }

            // Set the expected exception attribute to use for this test
            ExpectedExceptionBaseAttribute expectedException = expectedExceptions[0];

            return(expectedException);
        }