コード例 #1
0
        /// <summary>
        /// Asserts that a specific exception or a more derived type is thrown by the action.
        /// </summary>
        public static void Throws(Type pType, Action pAction, AssertionException pException = null)
        {
            if (pAction == null)
            {
                throw new ArgumentNullException("pAction");
            }
            if (pType == null)
            {
                throw new ArgumentNullException("pType");
            }
            if (pType != typeof(Exception) && !pType.IsSubclassOf(typeof(Exception)))
            {
                throw new ArgumentException("pType must derive from Exception", "pType");
            }

            bool thrown = false;

            try {
                pAction();
            }
            catch (Exception e) {
                thrown = true;
                Type thrownType = e.GetType();
                if (thrownType != pType && !thrownType.IsSubclassOf(pType))
                {
                    throw pException ?? AssertionException.GenerateWithInnerException(e, "Exception type was unexpected");
                }
            }

            if (!thrown)
            {
                throw pException ?? new AssertionException("Expected exception was not thrown");
            }
        }
コード例 #2
0
        /// <summary>
        /// Asserts that an action does not throw at all.
        /// </summary>
        public static void DoesNotThrow(Action pAction, AssertionException pException = null)
        {
            if (pAction == null)
            {
                throw new ArgumentNullException("pAction");
            }

            try {
                pAction();
            }
            catch (Exception e) {
                throw pException ?? AssertionException.GenerateWithInnerException(e, "Unexpected exception");
            }
        }