public void CheckForDefaultValueShouldReturnFalseIfValueIsNotDefaultForClass()
        {
            object obj    = TestObjectFactory.GetValidRequestModel();
            var    result = CommonValidator.CheckForDefaultValue(obj);

            Assert.False(result);
        }
        public void CallingShouldHaveValidModelStateWhenThereAreNoModelErrorsForPocoController()
        {
            MyMvc
            .IsUsingDefaultConfiguration()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            var requestModel = TestObjectFactory.GetValidRequestModel();

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

                Assert.True(modelState.IsValid);
                Assert.Equal(0, modelState.Values.Count());
                Assert.Equal(0, modelState.Keys.Count());
            });

            MyMvc.IsUsingDefaultConfiguration();
        }
コード例 #3
0
        public void CallingShouldHaveValidModelStateWhenThereAreNoModelErrorsForPocoController()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.AddHttpContextAccessor();
            });

            var requestModel = TestObjectFactory.GetValidRequestModel();

            MyController <FullPocoController>
            .Instance()
            .Calling(c => c.OkResultActionWithRequestBody(1, requestModel))
            .ShouldReturn()
            .Ok()
            .ShouldPassForThe <FullPocoController>(controller =>
            {
                var modelState = controller.CustomControllerContext.ModelState;

                Assert.True(modelState.IsValid);
                Assert.Empty(modelState.Values);
                Assert.Empty(modelState.Keys);
            });

            MyApplication.StartsFrom <DefaultStartup>();
        }
コード例 #4
0
        public void ShouldHaveValidModelStateShouldBeValidWithValidRequestModel()
        {
            var requestModel = TestObjectFactory.GetValidRequestModel();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestModel))
            .ShouldHave()
            .ValidModelState();
        }
コード例 #5
0
        public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModelAndProvidedNumberOfErrors()
        {
            var requestModel = TestObjectFactory.GetValidRequestModel();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestModel))
            .ShouldHave()
            .InvalidModelState(withNumberOfErrors: 5);
        }
コード例 #6
0
        public void WithModelStateWithErrorShouldWorkCorrectly()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            MyController <MvcController>
            .Instance()
            .WithModelState(modelState => modelState
                            .WithError("TestError", "Invalid value"))
            .Calling(c => c.BadRequestWithModelState(requestBody))
            .ShouldReturn()
            .BadRequest();
        }
コード例 #7
0
        public void ContainingNoErrorsShouldNotThrowExceptionWhenThereAreNoModelStateErrors()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.OkResultActionWithRequestBody(1, requestBody))
            .ShouldReturn()
            .Ok()
            .WithResponseModelOfType <ICollection <ResponseModel> >()
            .ContainingNoModelStateErrors();
        }
コード例 #8
0
        public void AndModelStateErrorShouldThrowExceptionWhenTheProvidedModelStateErrorDoesNotExist()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestBody))
            .ShouldReturn()
            .Ok()
            .WithResponseModel(requestBody)
            .ContainingModelStateError("Name");
        }
コード例 #9
0
        public void AndNoModelStateErrorForShouldNotThrowExceptionWhenTheProvidedPropertyDoesNotHaveErrors()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            MyWebApi
            .Controller <WebApiController>()
            .Calling(c => c.ModelStateCheck(requestBody))
            .ShouldReturn()
            .Ok()
            .WithResponseModel(requestBody)
            .ContainingNoModelStateErrorFor(r => r.RequiredString);
        }
コード例 #10
0
        public void AndShouldWorkCorrectlyWithValidModelState()
        {
            var requestModel = TestObjectFactory.GetValidRequestModel();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestModel))
            .ShouldHave()
            .ValidModelState()
            .AndAlso()
            .ShouldReturn()
            .Ok();
        }
コード例 #11
0
        public void AndNoModelStateErrorForShouldNotThrowExceptionWhenChainedWithValidModel()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestBody))
            .ShouldReturn()
            .Ok()
            .WithModel(requestBody)
            .ContainingNoErrorFor(r => r.Integer)
            .ContainingNoErrorFor(r => r.RequiredString);
        }
コード例 #12
0
        public void ContainingNoErrorsShouldNotThrowExceptionWhenThereAreNoModelStateErrors()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.OkResultActionWithRequestBody(1, requestBody))
            .ShouldHave()
            .ModelState(modelState => modelState
                        .ContainingNoErrors())
            .AndAlso()
            .ShouldReturn()
            .Ok(ok => ok
                .WithModelOfType <ICollection <ResponseModel> >());
        }
コード例 #13
0
        public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel()
        {
            var requestModel = TestObjectFactory.GetValidRequestModel();

            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                MyController <MvcController>
                .Instance()
                .Calling(c => c.ModelStateCheck(requestModel))
                .ShouldHave()
                .InvalidModelState();
            },
                "When calling ModelStateCheck action in MvcController expected to have invalid model state, but was in fact valid.");
        }
コード例 #14
0
        public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModelAndProvidedNumberOfErrors()
        {
            var requestModel = TestObjectFactory.GetValidRequestModel();

            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                MyController <MvcController>
                .Instance()
                .Calling(c => c.ModelStateCheck(requestModel))
                .ShouldHave()
                .InvalidModelState(withNumberOfErrors: 5);
            },
                "When calling ModelStateCheck action in MvcController expected to have invalid model state with 5 errors, but in fact contained 0.");
        }
コード例 #15
0
        public void AndNoModelStateErrorForShouldNotThrowExceptionWhenTheProvidedPropertyDoesNotHaveErrors()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.ModelStateCheck(requestBody))
            .ShouldHave()
            .ModelState(modelState => modelState
                        .For <RequestModel>()
                        .ContainingNoErrorFor(r => r.RequiredString))
            .AndAlso()
            .ShouldReturn()
            .Ok(ok => ok
                .WithModel(requestBody));
        }
コード例 #16
0
        public void CallingShouldHaveValidModelStateWhenThereAreNoModelErrors()
        {
            var requestModel = TestObjectFactory.GetValidRequestModel();

            var controller = MyWebApi
                             .Controller <WebApiController>()
                             .Calling(c => c.OkResultActionWithRequestBody(1, requestModel))
                             .ShouldReturn()
                             .Ok()
                             .AndProvideTheController();

            var modelState = controller.ModelState;

            Assert.IsTrue(modelState.IsValid);
            Assert.AreEqual(0, modelState.Values.Count);
            Assert.AreEqual(0, modelState.Keys.Count);
        }
コード例 #17
0
        public void WithModelStateWithErrorsDictionaryShouldWorkCorrectly()
        {
            var requestBody      = TestObjectFactory.GetValidRequestModel();
            var errorsDictionary = new Dictionary <string, string>()
            {
                ["First"]  = "SomeError",
                ["Second"] = "AnotherError",
            };

            MyController <MvcController>
            .Instance()
            .WithModelState(modelState => modelState
                            .WithErrors(errorsDictionary))
            .Calling(c => c.BadRequestWithModelState(requestBody))
            .ShouldReturn()
            .BadRequest();
        }
コード例 #18
0
        public void WithModelStateWithErrorsObjectShouldWorkCorrectly()
        {
            var requestBody  = TestObjectFactory.GetValidRequestModel();
            var errorsObjcet = new
            {
                First  = "SomeError",
                Second = "AnotherError",
            };

            MyController <MvcController>
            .Instance()
            .WithModelState(modelState => modelState
                            .WithErrors(errorsObjcet))
            .Calling(c => c.BadRequestWithModelState(requestBody))
            .ShouldReturn()
            .BadRequest();
        }
コード例 #19
0
        public void AndModelStateErrorForShouldThrowExceptionWhenTheProvidedPropertyDoesNotHaveErrors()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                MyController <MvcController>
                .Instance()
                .Calling(c => c.ModelStateCheck(requestBody))
                .ShouldReturn()
                .Ok()
                .WithModel(requestBody)
                .ContainingErrorFor(r => r.RequiredString);
            },
                "When calling ModelStateCheck action in MvcController expected to have a model error against key RequiredString, but none found.");
        }
コード例 #20
0
        public void AndModelStateErrorShouldThrowExceptionWhenTheProvidedModelStateErrorDoesNotExist()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                MyMvc
                .Controller <MvcController>()
                .Calling(c => c.ModelStateCheck(requestBody))
                .ShouldReturn()
                .Ok()
                .WithResponseModel(requestBody)
                .ContainingError("Name");
            },
                "When calling ModelStateCheck action in MvcController expected to have a model error against key Name, but none found.");
        }
コード例 #21
0
        public void CallingShouldHaveValidModelStateWhenThereAreNoModelErrors()
        {
            var requestModel = TestObjectFactory.GetValidRequestModel();

            MyController <MvcController>
            .Instance()
            .Calling(c => c.OkResultActionWithRequestBody(1, requestModel))
            .ShouldReturn()
            .Ok()
            .ShouldPassForThe <MvcController>(controller =>
            {
                var modelState = controller.ModelState;

                Assert.True(modelState.IsValid);
                Assert.Equal(0, modelState.Values.Count());
                Assert.Equal(0, modelState.Keys.Count());
            });
        }
コード例 #22
0
        public void AndModelStateErrorShouldThrowExceptionWhenTheProvidedModelStateErrorDoesNotExist()
        {
            var requestBody = TestObjectFactory.GetValidRequestModel();

            Test.AssertException <ModelErrorAssertionException>(
                () =>
            {
                MyController <MvcController>
                .Instance()
                .Calling(c => c.ModelStateCheck(requestBody))
                .ShouldHave()
                .ModelState(modelState => modelState
                            .ContainingError("Name"))
                .AndAlso()
                .ShouldReturn()
                .Ok(ok => ok
                    .WithModel(requestBody));
            },
                "When calling ModelStateCheck action in MvcController expected to have a model error against key 'Name', but in fact none was found.");
        }
コード例 #23
0
        public void ResolveMethodArgumentsShouldReturnCorrectCollectionOfArgumentsInformation()
        {
            var requestModel = TestObjectFactory.GetValidRequestModel();

            Expression <Func <WebApiController, IHttpActionResult> > expression = c => c.OkResultActionWithRequestBody(1, requestModel);

            var result = ExpressionParser.ResolveMethodArguments(expression);

            var arguments      = result as IList <MethodArgumentInfo> ?? result.ToArray();
            var firstArgument  = arguments.First();
            var secondArgument = arguments.Last();
            var secondArgumentAsRequestModel = secondArgument.Value as RequestModel;

            Assert.AreEqual("id", firstArgument.Name);
            Assert.AreEqual(typeof(int), firstArgument.Type);
            Assert.AreEqual(1, firstArgument.Value);

            Assert.AreEqual("model", secondArgument.Name);
            Assert.AreEqual(typeof(RequestModel), secondArgument.Type);
            Assert.IsNotNull(secondArgumentAsRequestModel);
            Assert.AreEqual(2, secondArgumentAsRequestModel.Integer);
            Assert.AreEqual("Test", secondArgumentAsRequestModel.RequiredString);
        }