Exemplo n.º 1
0
        /// <summary>
        /// Tests whether the tested <see cref="Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary"/> is valid.
        /// </summary>
        /// <param name="modelErrorTestBuilder">Instance of <see cref="IModelErrorTestBuilder"/> type.</param>
        /// <returns>Test builder of <see cref="IBaseTestBuilderWithInvokedAction"/> type.</returns>
        public static IBaseTestBuilderWithInvokedAction ContainingNoErrors(this IModelErrorTestBuilder modelErrorTestBuilder)
        {
            var actualModelErrorTestBuilder = (ModelErrorTestBuilder)modelErrorTestBuilder;

            actualModelErrorTestBuilder.CheckValidModelState();

            return(actualModelErrorTestBuilder.NewAndProvideTestBuilder());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tests whether the tested <see cref="Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary"/> is valid.
        /// </summary>
        /// <param name="modelErrorTestBuilder">Instance of <see cref="IModelErrorTestBuilder"/> type.</param>
        /// <returns>Test builder of <see cref="IAndTestBuilder"/> type.</returns>
        public static IAndTestBuilder ContainingNoErrors(this IModelErrorTestBuilder modelErrorTestBuilder)
        {
            var actualModelErrorTestBuilder = (ModelErrorTestBuilder)modelErrorTestBuilder;

            ModelStateValidator.CheckValidModelState(actualModelErrorTestBuilder.TestContext);

            return(new AndTestBuilder(actualModelErrorTestBuilder.TestContext));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Tests whether the tested <see cref="Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary"/> contains error by member expression.
        /// </summary>
        /// <typeparam name="TModel">Type of model which will be tested for no errors.</typeparam>
        /// <typeparam name="TMember">Type of the member which will be tested for errors.</typeparam>
        /// <param name="modelErrorTestBuilder">Instance of <see cref="IModelErrorTestBuilder{TModel}"/> type.</param>
        /// <param name="memberWithError">Member expression for the tested member.</param>
        /// <returns>Test builder of <see cref="IModelErrorDetailsTestBuilder{TModel}"/> type.</returns>
        public static IModelErrorDetailsTestBuilder <TModel> ContainingErrorFor <TModel, TMember>(
            this IModelErrorTestBuilder <TModel> modelErrorTestBuilder,
            Expression <Func <TModel, TMember> > memberWithError)
        {
            var actualModelErrorTestBuilder = (ModelErrorTestBuilder <TModel>)modelErrorTestBuilder;

            var memberName = ExpressionHelper.GetExpressionText(memberWithError, ExpressionTextCache);

            actualModelErrorTestBuilder.ContainingError(memberName);

            return(new ModelErrorDetailsTestBuilder <TModel>(
                       actualModelErrorTestBuilder.TestContext,
                       actualModelErrorTestBuilder,
                       memberName,
                       actualModelErrorTestBuilder.ModelState[memberName].Errors));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Tests whether the tested <see cref="Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary"/> contains no error by member expression.
        /// </summary>
        /// <typeparam name="TModel">Type of model which will be tested for no errors.</typeparam>
        /// <typeparam name="TMember">Type of the member which will be tested for no errors.</typeparam>
        /// <param name="modelErrorTestBuilder">Instance of <see cref="IModelErrorTestBuilder{TModel}"/> type.</param>
        /// <param name="memberWithNoError">Member expression for the tested member.</param>
        /// <returns>The same <see cref="IAndModelErrorTestBuilder{TModel}"/>.</returns>
        public static IAndModelErrorTestBuilder <TModel> ContainingNoErrorFor <TModel, TMember>(
            this IModelErrorTestBuilder <TModel> modelErrorTestBuilder,
            Expression <Func <TModel, TMember> > memberWithNoError)
        {
            var actualModelErrorTestBuilder = (ModelErrorTestBuilder <TModel>)modelErrorTestBuilder;

            var memberName = ExpressionHelper.GetExpressionText(memberWithNoError, ExpressionTextCache);

            if (actualModelErrorTestBuilder.ModelState.ContainsKey(memberName))
            {
                actualModelErrorTestBuilder.ThrowNewModelErrorAssertionException(
                    "{0} to have no model errors against key {1}, but found some.",
                    memberName);
            }

            return(actualModelErrorTestBuilder);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Tests whether the tested <see cref="Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary"/> contains error by key.
        /// </summary>
        /// <param name="modelErrorTestBuilder">Instance of <see cref="IModelErrorTestBuilder{TModel}"/> type.</param>
        /// <param name="errorKey">Error key to search for.</param>
        /// <returns>Test builder of <see cref="IModelErrorDetailsTestBuilder{TModel}"/> type.</returns>
        public static IModelErrorDetailsTestBuilder <TModel> ContainingError <TModel>(
            this IModelErrorTestBuilder <TModel> modelErrorTestBuilder,
            string errorKey)
        {
            var actualModelErrorTestBuilder = (ModelErrorTestBuilder <TModel>)modelErrorTestBuilder;

            if (!actualModelErrorTestBuilder.ModelState.ContainsKey(errorKey) ||
                actualModelErrorTestBuilder.ModelState.Count == 0)
            {
                actualModelErrorTestBuilder.ThrowNewModelErrorAssertionException(
                    "When calling {0} action in {1} expected to have a model error against key {2}, but none found.",
                    errorKey);
            }

            return(new ModelErrorDetailsTestBuilder <TModel>(
                       actualModelErrorTestBuilder.TestContext,
                       actualModelErrorTestBuilder,
                       errorKey,
                       actualModelErrorTestBuilder.ModelState[errorKey].Errors));
        }