public void WithModelStateForShouldThrowExceptionWhenModelStateHasSameErrors()
        {
            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
                var modelState             = new ModelStateDictionary();
                modelState.AddModelError("Integer", "The field Integer must be between 1 and 2147483647.");
                modelState.AddModelError("RequiredString", "The RequiredString field is required.");

                MyController <MvcController>
                .Instance()
                .Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
                .ShouldReturn()
                .BadRequest(badRequest => badRequest
                            .WithModelStateError(modelStateError => modelStateError
                                                 .For <RequestModel>()
                                                 .ContainingErrorFor(m => m.Integer).ThatEquals("The field Integer 1 must be between 1 and 2147483647.")
                                                 .AndAlso()
                                                 .ContainingErrorFor(m => m.RequiredString).BeginningWith("The RequiredString")
                                                 .AndAlso()
                                                 .ContainingErrorFor(m => m.RequiredString).EndingWith("required.")
                                                 .AndAlso()
                                                 .ContainingErrorFor(m => m.RequiredString).Containing("field")
                                                 .AndAlso()
                                                 .ContainingNoErrorFor(m => m.NonRequiredString)));
            },
                "When calling BadRequestWithModelState action in MvcController expected error message for key Integer to be 'The field Integer 1 must be between 1 and 2147483647.', but instead found 'The field Integer must be between 1 and 2147483647.'.");
        }
        public void WithModelStateForShouldNotThrowExceptionWhenModelStateHasSameErrors()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
            var modelState             = new ModelStateDictionary();

            modelState.AddModelError("Integer", "The field Integer must be between 1 and 2147483647.");
            modelState.AddModelError("RequiredString", "The RequiredString field is required.");

            MyController <MvcController>
            .Instance()
            .Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
            .ShouldReturn()
            .BadRequest(badRequest => badRequest
                        .WithModelStateError(modelStateError => modelStateError
                                             .For <RequestModel>()
                                             .ContainingErrorFor(m => m.Integer).ThatEquals("The field Integer must be between 1 and 2147483647.")
                                             .AndAlso()
                                             .ContainingErrorFor(m => m.RequiredString).BeginningWith("The RequiredString")
                                             .AndAlso()
                                             .ContainingErrorFor(m => m.RequiredString).EndingWith("required.")
                                             .AndAlso()
                                             .ContainingErrorFor(m => m.RequiredString).Containing("field")
                                             .AndAlso()
                                             .ContainingNoErrorFor(m => m.NonRequiredString)));
        }
        public void CallingShouldPopulateModelStateWhenThereAreModelErrorsForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            var requestModel = TestObjectFactory.GetRequestModelWithErrors();

            MyMvc
            .Controller <FullPocoController>()
            .Calling(c => c.OkResultActionWithRequestBody(1, requestModel))
            .ShouldReturn()
            .Ok()
            .ShouldPassFor()
            .TheController(controller =>
            {
                var modelState = (controller as FullPocoController).CustomControllerContext.ModelState;

                Assert.False(modelState.IsValid);
                Assert.Equal(2, modelState.Values.Count());
                Assert.Equal("Integer", modelState.Keys.First());
                Assert.Equal("RequiredString", modelState.Keys.Last());
            });

            MyMvc.IsUsingDefaultConfiguration();
        }
 public void WithoutValidationShouldNotValidateTheRequestModel()
 {
     MyWebApi
     .Controller <WebApiController>()
     .WithoutValidation()
     .Calling(c => c.ModelStateCheck(TestObjectFactory.GetRequestModelWithErrors()))
     .ShouldHave()
     .ValidModelState();
 }
Example #5
0
        public void ShouldHaveInvalidModelStateShouldBeInvalidWithInvalidRequestModelAndIncorrectNumberOfErrors()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .InvalidModelState(5);
        }
Example #6
0
        public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModel()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .InvalidModelState();
        }
        public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModelAndCorrectNumberOfErrors()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .InvalidModelState(2);
        }
Example #8
0
        public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsInvalidModelStateResult()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
            .ShouldReturn()
            .BadRequest();
        }
Example #9
0
        public void ShouldHaveValidModelStateShouldThrowExceptionWithInvalidRequestModel()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .ValidModelState();
        }
Example #10
0
        public void WithModelStateShouldNotThrowExceptionWhenModelStateHasDefaultErrors()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
            .ShouldReturn()
            .BadRequest()
            .WithModelStateError();
        }
Example #11
0
        public void AndModelStateErrorShouldNotThrowExceptionWhenTheProvidedModelStateErrorExists()
        {
            var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.ModelStateCheck(requestBodyWithErrors))
            .ShouldReturn()
            .Ok()
            .WithResponseModel(requestBodyWithErrors)
            .ContainingError("RequiredString");
        }
Example #12
0
        public void ContainingNoErrorsShouldThrowExceptionWhenThereAreModelStateErrors()
        {
            var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.OkResultActionWithRequestBody(1, requestBodyWithErrors))
            .ShouldReturn()
            .Ok()
            .WithResponseModelOfType <ICollection <ResponseModel> >()
            .ContainingNoModelStateErrors();
        }
Example #13
0
        public void AndModelStateErrorForShouldNotThrowExceptionWhenTheProvidedPropertyHasErrors()
        {
            var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestBodyWithErrors))
            .ShouldReturn()
            .Ok()
            .WithResponseModel(requestBodyWithErrors)
            .ContainingModelStateErrorFor(r => r.RequiredString);
        }
Example #14
0
        public void AndNoModelStateErrorForShouldThrowExceptionWhenChainedWithInvalidModel()
        {
            var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestBodyWithErrors))
            .ShouldReturn()
            .Ok()
            .WithResponseModel(requestBodyWithErrors)
            .ContainingNoModelStateErrorFor(r => r.Integer)
            .ContainingNoModelStateErrorFor(r => r.RequiredString);
        }
Example #15
0
        public void BeginningWithShouldNotThrowExceptionWhenProvidedMessageIsValid()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .ModelState(modelState => modelState.For <RequestModel>()
                        .ContainingNoErrorFor(m => m.NonRequiredString)
                        .ContainingErrorFor(m => m.RequiredString).BeginningWith("The RequiredString")
                        .ContainingErrorFor(m => m.Integer).BeginningWith("The field Integer"));
        }
        public void EngingWithShouldThrowExceptionWhenProvidedMessageIsValid()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .ModelStateFor <RequestModel>()
            .ContainingNoModelStateErrorFor(m => m.NonRequiredString)
            .ContainingModelStateErrorFor(m => m.RequiredString).EndingWith("required!")
            .ContainingModelStateErrorFor(m => m.Integer).EndingWith(string.Format("{0} and {1}!", 1, int.MaxValue));
        }
Example #17
0
        public void EngingWithShouldNotThrowExceptionWhenProvidedMessageIsValid()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .ModelState(modelState => modelState.For <RequestModel>()
                        .ContainingNoErrorFor(m => m.NonRequiredString)
                        .ContainingErrorFor(m => m.RequiredString).EndingWith("required.")
                        .ContainingErrorFor(m => m.Integer).EndingWith($"{1} and {int.MaxValue}."));
        }
        public void BeginningWithShouldThrowExceptionWhenProvidedMessageIsValid()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .ModelStateFor <RequestModel>()
            .ContainingNoModelStateErrorFor(m => m.NonRequiredString)
            .ContainingModelStateErrorFor(m => m.RequiredString).BeginningWith("RequiredString")
            .ContainingModelStateErrorFor(m => m.Integer).BeginningWith("Integer");
        }
        public void AndShouldWorkCorrectlyWithInvalidModelState()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .InvalidModelState()
            .AndAlso()
            .ShouldReturn()
            .Ok();
        }
        public void ShouldHaveModelStateForShouldChainCorrectly()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .ModelState(modelState => modelState.For <RequestModel>()
                        .ContainingNoErrorFor(r => r.NonRequiredString)
                        .ContainingErrorFor(r => r.Integer)
                        .ContainingErrorFor(r => r.RequiredString));
        }
        public void ThatEqualsShouldThrowExceptionWhenProvidedMessageIsValid()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .ModelStateFor <RequestModel>()
            .ContainingNoModelStateErrorFor(m => m.NonRequiredString)
            .AndAlso()
            .ContainingModelStateErrorFor(m => m.RequiredString).ThatEquals("RequiredString field is required.")
            .ContainingModelStateErrorFor(m => m.Integer).ThatEquals(string.Format("Integer must be between {0} and {1}.", 1, int.MaxValue));
        }
Example #22
0
        public void AndProvideModelShouldThrowExceptionWhenIsCalledOnTheRequest()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .ModelStateFor <RequestModel>()
            .ContainingNoModelStateErrorFor(r => r.NonRequiredString)
            .ContainingModelStateErrorFor(r => r.Integer)
            .ContainingModelStateErrorFor(r => r.RequiredString)
            .AndProvideTheModel();
        }
Example #23
0
        public void WithModelStateShouldThrowExceptionWhenModelStateHasLessNumberOfKeys()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
            var modelState             = new ModelStateDictionary();

            modelState.AddModelError("Integer", "The field Integer must be between 1 and 2147483647.");

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
            .ShouldReturn()
            .BadRequest()
            .WithModelState(modelState);
        }
        public void ShouldHaveInvalidModelStateShouldBeInvalidWithInvalidRequestModelAndIncorrectNumberOfErrors()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                MyController <MvcController>
                .Instance()
                .Calling(c => c.ModelStateCheck(requestModelWithErrors))
                .ShouldHave()
                .InvalidModelState(5);
            },
                "When calling ModelStateCheck action in MvcController expected to have invalid model state with 5 errors, but in fact contained 2.");
        }
Example #25
0
        public void WithModelStateShouldNotThrowExceptionWhenModelStateHasSameErrors()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
            var modelState             = new ModelStateDictionary();

            modelState.AddModelError("Integer", "The field Integer must be between 1 and 2147483647.");
            modelState.AddModelError("RequiredString", "The RequiredString field is required.");

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
            .ShouldReturn()
            .BadRequest()
            .WithModelStateError(modelState);
        }
        public void ShouldHaveValidModelStateShouldThrowExceptionWithInvalidRequestModel()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                MyController <MvcController>
                .Instance()
                .Calling(c => c.ModelStateCheck(requestModelWithErrors))
                .ShouldHave()
                .ValidModelState();
            },
                "When calling ModelStateCheck action in MvcController expected to have valid model state with no errors, but it had some.");
        }
        public void AndModelStateErrorForShouldNotThrowExceptionWhenTheProvidedPropertyHasErrors()
        {
            var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestBodyWithErrors))
            .ShouldHave()
            .ModelState(modelState => modelState
                        .For <RequestModel>()
                        .ContainingErrorFor(r => r.RequiredString))
            .AndAlso()
            .ShouldReturn()
            .Ok(ok => ok
                .WithModel(requestBodyWithErrors));
        }
Example #28
0
        public void WithErrorMessageShouldThrowExceptionWhenResultIsNotString()
        {
            Test.AssertException <BadRequestResultAssertionException>(
                () =>
            {
                var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

                MyMvc
                .Controller <MvcController>()
                .Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
                .ShouldReturn()
                .BadRequest()
                .WithErrorMessage("Good request");
            },
                "When calling BadRequestWithModelState action in MvcController expected bad request result with error message, but instead received non-string value.");
        }
Example #29
0
        public void AndNoModelStateErrorForShouldThrowExceptionWhenTheProvidedPropertyHasErrors()
        {
            var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                MyController <MvcController>
                .Instance()
                .Calling(c => c.ModelStateCheck(requestBodyWithErrors))
                .ShouldReturn()
                .Ok()
                .WithModel(requestBodyWithErrors)
                .ContainingNoErrorFor(r => r.RequiredString);
            },
                "When calling ModelStateCheck action in MvcController expected to have no model errors against key RequiredString, but found some.");
        }
Example #30
0
        public void AndModelStateErrorShouldNotThrowExceptionWhenTheProvidedModelStateErrorExists()
        {
            var requestBodyWithErrors = TestObjectFactory.GetRequestModelWithErrors();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestBodyWithErrors))
            .ShouldHave()
            .ModelState(modelState => modelState
                        .ContainingError("RequiredString")
                        .AndAlso()
                        .ContainingNoError("MissingError"))
            .AndAlso()
            .ShouldReturn()
            .Ok()
            .WithModel(requestBodyWithErrors);
        }