Esempio 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);
        }
Esempio n. 2
0
        public static IAssert <TSubject, TResult, TVars, TSequence> ItCallsInOrder <TSubject, TResult, TVars, TSequence> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            string text,
            Assertion <TSubject, TResult, TVars, TSequence> orderedAssertion)
        {
            var controller = assert.GetTestController();

            controller.Replace <Act>(
                (x, originalAction) =>
            {
                var scope = Fake.CreateScope();
                x[Key]    = scope;
                using (scope)
                {
                    originalAction();
                }
            });
            controller.AddAssertion(
                "calls in order " + text,
                x =>
            {
                var scope = (IFakeScope)x[Key];
                using (scope.OrderedAssertions())
                {
                    orderedAssertion(x);
                }
            });
            return(assert);
        }
Esempio n. 3
0
        public static IAssert <TSubject, TResult, TVars, TSequence> It <TSubject, TResult, TVars, TSequence> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            string text)
        {
            var controller = assert.GetTestController();

            controller.AddNotImplementedAssertion <Assert>(text);
            return(assert);
        }
Esempio n. 4
0
        public static IAssert <TSubject, TResult, TVars, TSequence> ItThrows <TSubject, TResult, TVars, TSequence> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            string text,
            Assertion <TSubject, TResult, TVars, TSequence> assertion)
        {
            var controller = assert.GetTestController();

            controller.AddAssertion("Throws " + text, assertion, c_expectException);
            return(assert);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        /// <summary>
        /// .ItReturns((DerivedType x) => x);
        /// </summary>
        public static IAssert <TSubject, TResult, TVars, TSequence> ItReturns <TSubject, TResult, TVars, TSequence, TDerivedResult> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            Action <TDerivedResult> resultAssertion)
            where TDerivedResult : class, TResult
        {
            var controller = assert.GetTestController();

            controller.AddAssertion(
                "Returns " + typeof(TDerivedResult).Name,
                x => resultAssertion(x.Result as TDerivedResult));
            return(assert);
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public static IAssert <TSubject, TResult, TVars, TSequence> ItThrows <TSubject, TResult, TVars, TSequence, TException> (
            this IAssert <TSubject, TResult, TVars, TSequence> assert,
            Action <TException> exceptionAssertion)
            where TException : Exception
        {
            var controller = assert.GetTestController();

            controller.AddAssertion(
                "Throws " + typeof(TException).Name,
                x => exceptionAssertion(x.Exception as TException),
                c_expectException);
            return(assert);
        }
Esempio n. 9
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);
        }