From 3cd5c17da7e0d14271244cf65a196aa99a520eb5 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 29 Sep 2014 08:55:26 -0700 Subject: [PATCH] Updating ModelBinding tests to work in Mono --- .../Binders/ByteArrayModelBinderTests.cs | 12 ++++--- .../Binders/MutableObjectModelBinderTest.cs | 21 ++++++------ .../Binders/TypeConverterModelBinderTest.cs | 32 ++++++++++--------- .../Utils/ValidationAttributeUtil.cs | 17 ++++++++++ .../Validation/CompareAttributeTest.cs | 6 ++++ .../MaxLengthAttributeAdapterTest.cs | 2 +- .../MinLengthAttributeAdapterTest.cs | 2 +- .../Validation/ModelValidationNodeTest.cs | 2 +- .../RequiredAttributeAdapterTest.cs | 3 +- .../StringLengthAttributeAdapterTest.cs | 6 ++-- .../project.json | 2 +- 11 files changed, 68 insertions(+), 37 deletions(-) create mode 100644 test/Microsoft.AspNet.Mvc.ModelBinding.Test/Utils/ValidationAttributeUtil.cs diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ByteArrayModelBinderTests.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ByteArrayModelBinderTests.cs index b21f90daf6..44305725d8 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ByteArrayModelBinderTests.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ByteArrayModelBinderTests.cs @@ -5,6 +5,7 @@ using System; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNet.Testing; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding.Test @@ -58,6 +59,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test public async Task BindModelAddsModelErrorsOnInvalidCharacters() { // Arrange + var expected = TestPlatformHelper.IsMono ? + "Invalid length." : + "The input is not a valid Base-64 string as it contains a non-base 64 character," + + " more than two padding characters, or an illegal character among the padding characters. "; + var valueProvider = new SimpleHttpValueProvider() { { "foo", "\"Fys1\"" } @@ -72,10 +78,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test // Assert Assert.True(binderResult); Assert.False(bindingContext.ModelState.IsValid); - Assert.Equal(1, bindingContext.ModelState.Values.Count); - Assert.Equal("The input is not a valid Base-64 string as it contains a non-base 64 character," + - " more than two padding characters, or an illegal character among the padding characters. ", - bindingContext.ModelState.Values.First().Errors[0].ErrorMessage); + var error = Assert.Single(bindingContext.ModelState["foo"].Errors); + Assert.Equal(expected, error.ErrorMessage); } [Fact] diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/MutableObjectModelBinderTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/MutableObjectModelBinderTest.cs index f3325a16ec..15781f6247 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/MutableObjectModelBinderTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/MutableObjectModelBinderTest.cs @@ -176,9 +176,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding .Returns(new Person()).Verifiable(); // Act - object originalModel = bindingContext.Model; + var originalModel = bindingContext.Model; testableBinder.Object.EnsureModelPublic(bindingContext); - object newModel = bindingContext.Model; + var newModel = bindingContext.Model; // Assert Assert.Null(originalModel); @@ -430,7 +430,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding var modelError = modelState.Errors[0]; Assert.Null(modelError.Exception); Assert.NotNull(modelError.ErrorMessage); - Assert.Equal("The Age field is required.", modelError.ErrorMessage); + var expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.Age)); + Assert.Equal(expected, modelError.ErrorMessage); // Check City error. Assert.True(modelStateDictionary.TryGetValue("theModel.City", out modelState)); @@ -439,7 +440,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding modelError = modelState.Errors[0]; Assert.Null(modelError.Exception); Assert.NotNull(modelError.ErrorMessage); - Assert.Equal("The City field is required.", modelError.ErrorMessage); + expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.City)); + Assert.Equal(expected, modelError.ErrorMessage); } [Fact] @@ -478,7 +480,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding var modelError = modelState.Errors[0]; Assert.Null(modelError.Exception); Assert.NotNull(modelError.ErrorMessage); - Assert.Equal("The City field is required.", modelError.ErrorMessage); + var expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.City)); + Assert.Equal(expected, modelError.ErrorMessage); } [Fact] @@ -521,8 +524,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding var bindingContext = CreateContext(containerMetadata); - ComplexModelDto dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties); - TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder(); + var dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties); + var testableBinder = new TestableMutableObjectModelBinder(); // Make ValueTypeRequired invalid. var propertyMetadata = dto.PropertyMetadata.Single(p => p.PropertyName == "ValueTypeRequired"); @@ -533,7 +536,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding testableBinder.ProcessDto(bindingContext, dto); // Assert - ModelStateDictionary modelStateDictionary = bindingContext.ModelState; + var modelStateDictionary = bindingContext.ModelState; Assert.Equal(false, modelStateDictionary.IsValid); Assert.Equal(1, modelStateDictionary.Count); @@ -542,7 +545,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding Assert.True(modelStateDictionary.TryGetValue("theModel.ValueTypeRequired", out modelState)); Assert.Equal(1, modelState.Errors.Count); - ModelError modelError = modelState.Errors[0]; + var modelError = modelState.Errors[0]; Assert.Null(modelError.Exception); Assert.NotNull(modelError.ErrorMessage); Assert.Equal("Sample message", modelError.ErrorMessage); diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/TypeConverterModelBinderTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/TypeConverterModelBinderTest.cs index 8c8062235a..4ed7d233c7 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/TypeConverterModelBinderTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/TypeConverterModelBinderTest.cs @@ -2,9 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.ComponentModel; using System.Globalization; using System.Threading.Tasks; +using Microsoft.AspNet.Testing; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding.Test @@ -62,34 +62,36 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() { // Arrange - ModelBindingContext bindingContext = GetBindingContext(typeof(int)); + var message = TestPlatformHelper.IsMono ? "Input string was not in the correct format" : + "Input string was not in a correct format."; + var bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "not an integer" } }; - TypeConverterModelBinder binder = new TypeConverterModelBinder(); + var binder = new TypeConverterModelBinder(); // Act - bool retVal = await binder.BindModelAsync(bindingContext); + var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.True(retVal); Assert.Null(bindingContext.Model); Assert.Equal(false, bindingContext.ModelState.IsValid); - Assert.Equal("Input string was not in a correct format.", bindingContext.ModelState["theModelName"].Errors[0].ErrorMessage); + var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors); + Assert.Equal(message, error.ErrorMessage); } [Fact] public async Task BindModel_NullValueProviderResult_ReturnsFalse() { // Arrange - ModelBindingContext bindingContext = GetBindingContext(typeof(int)); - - TypeConverterModelBinder binder = new TypeConverterModelBinder(); + var bindingContext = GetBindingContext(typeof(int)); + var binder = new TypeConverterModelBinder(); // Act - bool retVal = await binder.BindModelAsync(bindingContext); + var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.False(retVal, "BindModel should have returned null."); @@ -100,16 +102,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test public async Task BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull() { // Arrange - ModelBindingContext bindingContext = GetBindingContext(typeof(string)); + var bindingContext = GetBindingContext(typeof(string)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "" } }; - TypeConverterModelBinder binder = new TypeConverterModelBinder(); + var binder = new TypeConverterModelBinder(); // Act - bool retVal = await binder.BindModelAsync(bindingContext); + var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.True(retVal); @@ -121,16 +123,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test public async Task BindModel_ValidValueProviderResult_ReturnsModel() { // Arrange - ModelBindingContext bindingContext = GetBindingContext(typeof(int)); + var bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "42" } }; - TypeConverterModelBinder binder = new TypeConverterModelBinder(); + var binder = new TypeConverterModelBinder(); // Act - bool retVal = await binder.BindModelAsync(bindingContext); + var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.True(retVal); diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Utils/ValidationAttributeUtil.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Utils/ValidationAttributeUtil.cs new file mode 100644 index 0000000000..6db239e0ee --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Utils/ValidationAttributeUtil.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.ComponentModel.DataAnnotations; + +namespace Microsoft.AspNet.Mvc.ModelBinding +{ + public static class ValidationAttributeUtil + { + public static string GetRequiredErrorMessage(string field) + { + var attr = new RequiredAttribute(); + return attr.FormatErrorMessage(field); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/CompareAttributeTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/CompareAttributeTest.cs index 43190e4a36..5135f805d2 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/CompareAttributeTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/CompareAttributeTest.cs @@ -71,6 +71,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding [Fact] public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { + if (TestPlatformHelper.IsMono) + { + // ValidationAttribute in Mono does not read non-public resx properties. + return; + } + // Arrange var metadataProvider = new DataAnnotationsModelMetadataProvider(); var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MaxLengthAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MaxLengthAttributeAdapterTest.cs index 4052cc7db9..d0878a1bc6 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MaxLengthAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MaxLengthAttributeAdapterTest.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding Assert.Equal("maxlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(10, rule.ValidationParameters["max"]); - Assert.Equal("The field Length must be a string or array type with a maximum length of '10'.", rule.ErrorMessage); + Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); } [Fact] diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MinLengthAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MinLengthAttributeAdapterTest.cs index fa9bfc9fdc..70eb2a4a57 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MinLengthAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MinLengthAttributeAdapterTest.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding Assert.Equal("minlength", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(6, rule.ValidationParameters["min"]); - Assert.Equal("The field Length must be a string or array type with a minimum length of '6'.", rule.ErrorMessage); + Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); } [Fact] diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/ModelValidationNodeTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/ModelValidationNodeTest.cs index 7c3b1f988e..66b8f7e607 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/ModelValidationNodeTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/ModelValidationNodeTest.cs @@ -305,7 +305,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding // Assert Assert.Equal(3, context.ModelState.Count); Assert.IsType(context.ModelState[""].Errors[0].Exception); - Assert.Equal("The RequiredString field is required.", + Assert.Equal(ValidationAttributeUtil.GetRequiredErrorMessage("RequiredString"), context.ModelState["theKey.RequiredString"].Errors[0].ErrorMessage); Assert.False(context.ModelState.ContainsKey("theKey.RangedInt")); Assert.False(context.ModelState.ContainsKey("theKey.ValidString")); diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/RequiredAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/RequiredAttributeAdapterTest.cs index 75dd1dc5a5..d4fff33eb2 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/RequiredAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/RequiredAttributeAdapterTest.cs @@ -14,6 +14,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding public void GetClientValidationRules_ReturnsValidationParameters() { // Arrange + var expected = ValidationAttributeUtil.GetRequiredErrorMessage("Length"); var provider = new DataAnnotationsModelMetadataProvider(); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var attribute = new RequiredAttribute(); @@ -27,7 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding var rule = Assert.Single(rules); Assert.Equal("required", rule.ValidationType); Assert.Empty(rule.ValidationParameters); - Assert.Equal("The Length field is required.", rule.ErrorMessage); + Assert.Equal(expected, rule.ErrorMessage); } } } diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/StringLengthAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/StringLengthAttributeAdapterTest.cs index 3f49a36693..52c758fe84 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/StringLengthAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/StringLengthAttributeAdapterTest.cs @@ -28,8 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding Assert.Equal("length", rule.ValidationType); Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(8, rule.ValidationParameters["max"]); - Assert.Equal("The field Length must be a string with a maximum length of 8.", - rule.ErrorMessage); + Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); } [Fact] @@ -52,8 +51,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal(3, rule.ValidationParameters["min"]); Assert.Equal(10, rule.ValidationParameters["max"]); - Assert.Equal("The field Length must be a string with a minimum length of 3 and a maximum length of 10.", - rule.ErrorMessage); + Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage); } } } diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/project.json b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/project.json index c6a1ab9b00..b4842180d9 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/project.json +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/project.json @@ -1,6 +1,6 @@ { "compilationOptions": { - "warningsAsErrors": true + "warningsAsErrors": "true" }, "dependencies": { "Microsoft.AspNet.Http": "1.0.0-*",