Пример #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);
        }
Пример #2
0
        /// <summary>
        /// .ItReturns(x => x.Exception);
        /// </summary>
        public static IAssert <TSubject, TResult, TVars, TSequence> ItReturns <TSubject, TResult, TVars, TSequence> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            Expression <Func <TVars, TResult> > resultProvider)
        {
            var controller = assert.GetTestController();

            controller.AddAssertion(
                "Returns " + resultProvider,
                x => AssertionHelper.AssertObjectEquals("Result", resultProvider.Compile()(x.Vars), x.Result));
            return(assert);
        }
Пример #3
0
        public static IAssert <TSubject, TResult, TVars, TSequence> ItThrows <TSubject, TResult, TVars, TSequence, TException> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            Expression <Func <TVars, TException> > exceptionProvider)
            where TException : Exception
        {
            var controller = assert.GetTestController();

            controller.AddAssertion(
                "Throws " + exceptionProvider,
                x => AssertionHelper.AssertObjectEquals("Exception", exceptionProvider.Compile()(x.Vars), x.Exception),
                c_expectException);
            return(assert);
        }
Пример #4
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);
        }