Exemplo n.º 1
0
        public static IAssert <TSubject, TResult, TVars, TSequence> ItThrows <TSubject, TResult, TVars, TSequence> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            Type exceptionType,
            [CanBeNull] Func <TVars, string> messageProvider           = null,
            [CanBeNull] Func <TVars, Exception> innerExceptionProvider = null)
        {
            var controller = assert.GetTestController();

            controller.AddAssertion(
                "Throws " + exceptionType.Name,
                x =>
            {
                AssertionHelper.AssertInstanceOfType("Exception", exceptionType, x.Exception);
                if (messageProvider != null)
                {
                    AssertionHelper.AssertExceptionMessage(messageProvider(x.Vars), x.Exception);
                }
                if (innerExceptionProvider != null)
                {
                    AssertionHelper.AssertObjectEquals("InnerException", innerExceptionProvider(x.Vars), x.Exception.AssertNotNull().InnerException);
                }
            },
                c_expectException);
            return(assert);
        }
Exemplo n.º 2
0
        public static IAssert <TSubject, TResult, TVars, TSequence> ItThrows <TSubject, TResult, TVars, TSequence> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            Type exceptionType,
            [CanBeNull] string message = null,
            [CanBeNull][RegexPattern] string messagePattern            = null,
            [CanBeNull] Func <TVars, string> messageProvider           = null,
            [CanBeNull] Func <TVars, Exception> innerExceptionProvider = null)
        {
            var controller = assert.GetTestController();

            controller.AddAssertion(
                "Throws " + exceptionType.Name,
                x =>
            {
                AssertionHelper.AssertInstanceOfType("Exception", exceptionType, x.Exception);

                if (message == null && messageProvider != null)
                {
                    message = messageProvider(x.Vars);
                }
                if (message != null && x.Exception.Message != message)
                {
                    throw new Exception($"Exception message must be '{message}' but was '{x.Exception.Message}'.");
                }
                if (messagePattern != null && Regex.IsMatch(x.Exception.Message, messagePattern))
                {
                    throw new Exception($"Exception message must match '{messagePattern}' but was '{x.Exception.Message}'.");
                }

                if (innerExceptionProvider != null)
                {
                    AssertionHelper.AssertObjectEquals("InnerException", innerExceptionProvider(x.Vars), x.Exception.NotNull().InnerException);
                }
            },
                expectException: true);
            return(assert);
        }