public void Validate_DoesNotThrow_ForValidModels() { // Arrange HttpConfiguration configuration = new HttpConfiguration(); TestController controller = new TestController { Configuration = configuration }; TestEntity entity = new TestEntity { ID = 42 }; // Act && Assert Assert.DoesNotThrow(() => controller.Validate(entity)); }
public void Validate_SetsModelStateErrorsUnderRightPrefix_ForInvalidModels() { // Arrange HttpConfiguration configuration = new HttpConfiguration(); TestController controller = new TestController { Configuration = configuration }; TestEntity entity = new TestEntity { ID = -1 }; // Act controller.Validate(entity, keyPrefix: "prefix"); // Assert Assert.False(controller.ModelState.IsValid); Assert.Equal("The field ID must be between 0 and 100.", controller.ModelState["prefix.ID"].Errors[0].ErrorMessage); }
public void Validate_DoesNothing_IfValidatorIsNull() { // Arrange HttpConfiguration configuration = new HttpConfiguration(); configuration.Services.Replace(typeof(IBodyModelValidator), null); TestEntity entity = new TestEntity { ID = 9999999 }; TestController controller = new TestController { Configuration = configuration }; // Act controller.Validate(entity); // Assert Assert.True(controller.ModelState.IsValid); }
public void Validate_CallsValidateOnConfiguredValidator_UsingConfiguredMetadataProvider() { // Arrange Mock<IBodyModelValidator> validator = new Mock<IBodyModelValidator>(); Mock<ModelMetadataProvider> metadataProvider = new Mock<ModelMetadataProvider>(); HttpConfiguration configuration = new HttpConfiguration(); configuration.Services.Replace(typeof(IBodyModelValidator), validator.Object); configuration.Services.Replace(typeof(ModelMetadataProvider), metadataProvider.Object); TestController controller = new TestController { Configuration = configuration }; TestEntity entity = new TestEntity { ID = 42 }; // Act controller.Validate(entity); // Assert validator.Verify( v => v.Validate(entity, typeof(TestEntity), metadataProvider.Object, controller.ActionContext, String.Empty), Times.Once()); Assert.True(controller.ModelState.IsValid); }
public void Validate_ThrowsInvalidOperationException_IfConfigurationIsNull() { // Arrange TestController controller = new TestController(); TestEntity entity = new TestEntity(); // Act & Assert Assert.Throws<InvalidOperationException>( () => controller.Validate(entity), "ApiController.Configuration must not be null."); }