コード例 #1
0
        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.'.");
        }
コード例 #2
0
        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();
        }
コード例 #4
0
 public void WithoutValidationShouldNotValidateTheRequestModel()
 {
     MyWebApi
     .Controller <WebApiController>()
     .WithoutValidation()
     .Calling(c => c.ModelStateCheck(TestObjectFactory.GetRequestModelWithErrors()))
     .ShouldHave()
     .ValidModelState();
 }
コード例 #5
0
        public void ShouldHaveInvalidModelStateShouldBeInvalidWithInvalidRequestModelAndIncorrectNumberOfErrors()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

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

            MyMvc
            .Controller <MvcController>()
            .Calling(c => c.ModelStateCheck(requestModelWithErrors))
            .ShouldHave()
            .InvalidModelState();
        }
コード例 #7
0
        public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModelAndCorrectNumberOfErrors()
        {
            var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();

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

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

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

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

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

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.OkResultActionWithRequestBody(1, requestBodyWithErrors))
            .ShouldReturn()
            .Ok()
            .WithResponseModelOfType <ICollection <ResponseModel> >()
            .ContainingNoModelStateErrors();
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #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"));
        }
コード例 #16
0
        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));
        }
コード例 #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}."));
        }
コード例 #18
0
        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");
        }
コード例 #19
0
        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));
        }
コード例 #21
0
        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));
        }
コード例 #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();
        }
コード例 #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);
        }
コード例 #24
0
        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.");
        }
コード例 #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);
        }
コード例 #26
0
        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.");
        }
コード例 #27
0
        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));
        }
コード例 #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.");
        }
コード例 #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.");
        }
コード例 #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);
        }