Exemple #1
0
        public static IRouteContext RouteContext <TController>(this Expression <Func <TController, ActionResult> > action)
            where TController : Controller
        {
            var controllerName = RouteDataFactory.ControllerName <TController>();
            var actionName     = action.ActionName();

            var routeValues = new RouteValueDictionary();

            action.GetMethodArgumentValues()
            .ForEach(arg =>
                     routeValues.Add(arg.Key.Name, arg.Value)
                     );

            return(new DerivedRouteContext(controllerName, actionName, routeValues));
        }
        /// <summary>
        ///     Asserts that a <see cref="RedirectToRouteResult">redirectResult</see> redirects to a specified
        ///     <typeparamref name="TController">controller</typeparamref>.
        /// </summary>
        /// <typeparam name="TController">The type of controller.</typeparam>
        /// <param name="because">
        ///     A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        ///     is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="reasonArgs">
        ///     Zero or more objects to format using the placeholders in <see cref="because" />.
        /// </param>
        public AndConstraint <RedirectToRouteResultAssertions> RedirectToController <TController>(string because = "",
                                                                                                  params object[]
                                                                                                  reasonArgs)
            where TController : IController
        {
            const string key = "Controller";
            var          expectedController = RouteDataFactory.ControllerName <TController>();

            if (ReferenceEquals(Subject, null))
            {
                Execute.Assertion
                .BecauseOf(because, reasonArgs)
                .FailWith(
                    "Expected {context:redirecttorouteresult} to redirect to controller {0}{reason}, but {context:redirecttorouteresult} was <null>.",
                    expectedController);
            }


            Execute.Assertion
            .BecauseOf(because, reasonArgs)
            .ForCondition(Subject.RouteValues.ContainsKey(key))
            .FailWith(
                "Expected {context:redirecttorouteresult} to to have route parameter {0}{reason}, but parameter {0} was not found.",
                key
                );

            var actualController = Subject.RouteValues.ContainsKey(key) &&
                                   !string.IsNullOrEmpty(Subject.RouteValues[key].IfExists(val => val.ToString()))
                ? Subject.RouteValues[key].IfExists(val => val.ToString())
                : null;

            Execute.Assertion
            .BecauseOf(because, reasonArgs)
            .ForCondition(
                string.Compare(expectedController,
                               actualController,
                               StringComparison.InvariantCultureIgnoreCase) ==
                0)
            .FailWith(
                "Expected {context:redirecttorouteresult} to redirect to controller {0}{reason}, but was {1}.",
                expectedController,
                actualController
                );

            return(new AndConstraint <RedirectToRouteResultAssertions>(this));
        }
Exemple #3
0
        /// <summary>
        ///     Asserts that a <see cref="RouteValueDictionary">routeValueDictionary</see> maps to a specified
        ///     <typeparamref name="TController">controller</typeparamref>.
        /// </summary>
        /// <typeparam name="TController">The type of Controller.</typeparam>
        /// <param name="because">
        ///     A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        ///     is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="reasonArgs">
        ///     Zero or more objects to format using the placeholders in <see cref="because" />.
        /// </param>
        public AndConstraint <RouteValueDictionaryAssertions> MapTo <TController>(string because = "",
                                                                                  params object[] reasonArgs)
            where TController : IController
        {
            if (ReferenceEquals(Subject, null))
            {
                Execute.Assertion
                .BecauseOf(because, reasonArgs)
                .FailWith("Expected {context:routevalues} to not be <null>{reason}.");
            }

            var expectedController = RouteDataFactory.ControllerName <TController>();

            Subject.Should().MapToController(expectedController, because, reasonArgs);

            return(new AndConstraint <RouteValueDictionaryAssertions>(this));
        }