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(); }
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>(); }
public void ShouldHaveValidModelStateShouldBeValidWithValidRequestModel() { var requestModel = TestObjectFactory.GetValidRequestModel(); MyController <MvcController> .Instance() .Calling(c => c.ModelStateCheck(requestModel)) .ShouldHave() .ValidModelState(); }
public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModelAndProvidedNumberOfErrors() { var requestModel = TestObjectFactory.GetValidRequestModel(); MyWebApi .Controller <WebApiController>() .Calling(c => c.ModelStateCheck(requestModel)) .ShouldHave() .InvalidModelState(withNumberOfErrors: 5); }
public void WithModelStateWithErrorShouldWorkCorrectly() { var requestBody = TestObjectFactory.GetValidRequestModel(); MyController <MvcController> .Instance() .WithModelState(modelState => modelState .WithError("TestError", "Invalid value")) .Calling(c => c.BadRequestWithModelState(requestBody)) .ShouldReturn() .BadRequest(); }
public void ContainingNoErrorsShouldNotThrowExceptionWhenThereAreNoModelStateErrors() { var requestBody = TestObjectFactory.GetValidRequestModel(); MyWebApi .Controller <WebApiController>() .Calling(c => c.OkResultActionWithRequestBody(1, requestBody)) .ShouldReturn() .Ok() .WithResponseModelOfType <ICollection <ResponseModel> >() .ContainingNoModelStateErrors(); }
public void AndModelStateErrorShouldThrowExceptionWhenTheProvidedModelStateErrorDoesNotExist() { var requestBody = TestObjectFactory.GetValidRequestModel(); MyWebApi .Controller <WebApiController>() .Calling(c => c.ModelStateCheck(requestBody)) .ShouldReturn() .Ok() .WithResponseModel(requestBody) .ContainingModelStateError("Name"); }
public void AndNoModelStateErrorForShouldNotThrowExceptionWhenTheProvidedPropertyDoesNotHaveErrors() { var requestBody = TestObjectFactory.GetValidRequestModel(); MyWebApi .Controller <WebApiController>() .Calling(c => c.ModelStateCheck(requestBody)) .ShouldReturn() .Ok() .WithResponseModel(requestBody) .ContainingNoModelStateErrorFor(r => r.RequiredString); }
public void AndShouldWorkCorrectlyWithValidModelState() { var requestModel = TestObjectFactory.GetValidRequestModel(); MyController <MvcController> .Instance() .Calling(c => c.ModelStateCheck(requestModel)) .ShouldHave() .ValidModelState() .AndAlso() .ShouldReturn() .Ok(); }
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); }
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> >()); }
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."); }
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."); }
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)); }
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); }
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(); }
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(); }
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."); }
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."); }
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()); }); }
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."); }
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); }