Updating ModelBinding tests to work in Mono

This commit is contained in:
Pranav K 2014-09-29 08:55:26 -07:00
parent 497274a404
commit 3cd5c17da7
11 changed files with 68 additions and 37 deletions

View File

@ -5,6 +5,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding.Test namespace Microsoft.AspNet.Mvc.ModelBinding.Test
@ -58,6 +59,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
public async Task BindModelAddsModelErrorsOnInvalidCharacters() public async Task BindModelAddsModelErrorsOnInvalidCharacters()
{ {
// Arrange // 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() var valueProvider = new SimpleHttpValueProvider()
{ {
{ "foo", "\"Fys1\"" } { "foo", "\"Fys1\"" }
@ -72,10 +78,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
// Assert // Assert
Assert.True(binderResult); Assert.True(binderResult);
Assert.False(bindingContext.ModelState.IsValid); Assert.False(bindingContext.ModelState.IsValid);
Assert.Equal(1, bindingContext.ModelState.Values.Count); var error = Assert.Single(bindingContext.ModelState["foo"].Errors);
Assert.Equal("The input is not a valid Base-64 string as it contains a non-base 64 character," + Assert.Equal(expected, error.ErrorMessage);
" more than two padding characters, or an illegal character among the padding characters. ",
bindingContext.ModelState.Values.First().Errors[0].ErrorMessage);
} }
[Fact] [Fact]

View File

@ -176,9 +176,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
.Returns(new Person()).Verifiable(); .Returns(new Person()).Verifiable();
// Act // Act
object originalModel = bindingContext.Model; var originalModel = bindingContext.Model;
testableBinder.Object.EnsureModelPublic(bindingContext); testableBinder.Object.EnsureModelPublic(bindingContext);
object newModel = bindingContext.Model; var newModel = bindingContext.Model;
// Assert // Assert
Assert.Null(originalModel); Assert.Null(originalModel);
@ -430,7 +430,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
var modelError = modelState.Errors[0]; var modelError = modelState.Errors[0];
Assert.Null(modelError.Exception); Assert.Null(modelError.Exception);
Assert.NotNull(modelError.ErrorMessage); 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. // Check City error.
Assert.True(modelStateDictionary.TryGetValue("theModel.City", out modelState)); Assert.True(modelStateDictionary.TryGetValue("theModel.City", out modelState));
@ -439,7 +440,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
modelError = modelState.Errors[0]; modelError = modelState.Errors[0];
Assert.Null(modelError.Exception); Assert.Null(modelError.Exception);
Assert.NotNull(modelError.ErrorMessage); 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] [Fact]
@ -478,7 +480,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
var modelError = modelState.Errors[0]; var modelError = modelState.Errors[0];
Assert.Null(modelError.Exception); Assert.Null(modelError.Exception);
Assert.NotNull(modelError.ErrorMessage); 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] [Fact]
@ -521,8 +524,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
var bindingContext = CreateContext(containerMetadata); var bindingContext = CreateContext(containerMetadata);
ComplexModelDto dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties); var dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties);
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder(); var testableBinder = new TestableMutableObjectModelBinder();
// Make ValueTypeRequired invalid. // Make ValueTypeRequired invalid.
var propertyMetadata = dto.PropertyMetadata.Single(p => p.PropertyName == "ValueTypeRequired"); var propertyMetadata = dto.PropertyMetadata.Single(p => p.PropertyName == "ValueTypeRequired");
@ -533,7 +536,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
testableBinder.ProcessDto(bindingContext, dto); testableBinder.ProcessDto(bindingContext, dto);
// Assert // Assert
ModelStateDictionary modelStateDictionary = bindingContext.ModelState; var modelStateDictionary = bindingContext.ModelState;
Assert.Equal(false, modelStateDictionary.IsValid); Assert.Equal(false, modelStateDictionary.IsValid);
Assert.Equal(1, modelStateDictionary.Count); Assert.Equal(1, modelStateDictionary.Count);
@ -542,7 +545,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.True(modelStateDictionary.TryGetValue("theModel.ValueTypeRequired", out modelState)); Assert.True(modelStateDictionary.TryGetValue("theModel.ValueTypeRequired", out modelState));
Assert.Equal(1, modelState.Errors.Count); Assert.Equal(1, modelState.Errors.Count);
ModelError modelError = modelState.Errors[0]; var modelError = modelState.Errors[0];
Assert.Null(modelError.Exception); Assert.Null(modelError.Exception);
Assert.NotNull(modelError.ErrorMessage); Assert.NotNull(modelError.ErrorMessage);
Assert.Equal("Sample message", modelError.ErrorMessage); Assert.Equal("Sample message", modelError.ErrorMessage);

View File

@ -2,9 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding.Test namespace Microsoft.AspNet.Mvc.ModelBinding.Test
@ -62,34 +62,36 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState()
{ {
// Arrange // 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 bindingContext.ValueProvider = new SimpleHttpValueProvider
{ {
{ "theModelName", "not an integer" } { "theModelName", "not an integer" }
}; };
TypeConverterModelBinder binder = new TypeConverterModelBinder(); var binder = new TypeConverterModelBinder();
// Act // Act
bool retVal = await binder.BindModelAsync(bindingContext); var retVal = await binder.BindModelAsync(bindingContext);
// Assert // Assert
Assert.True(retVal); Assert.True(retVal);
Assert.Null(bindingContext.Model); Assert.Null(bindingContext.Model);
Assert.Equal(false, bindingContext.ModelState.IsValid); 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] [Fact]
public async Task BindModel_NullValueProviderResult_ReturnsFalse() public async Task BindModel_NullValueProviderResult_ReturnsFalse()
{ {
// Arrange // Arrange
ModelBindingContext bindingContext = GetBindingContext(typeof(int)); var bindingContext = GetBindingContext(typeof(int));
var binder = new TypeConverterModelBinder();
TypeConverterModelBinder binder = new TypeConverterModelBinder();
// Act // Act
bool retVal = await binder.BindModelAsync(bindingContext); var retVal = await binder.BindModelAsync(bindingContext);
// Assert // Assert
Assert.False(retVal, "BindModel should have returned null."); Assert.False(retVal, "BindModel should have returned null.");
@ -100,16 +102,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
public async Task BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull() public async Task BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull()
{ {
// Arrange // Arrange
ModelBindingContext bindingContext = GetBindingContext(typeof(string)); var bindingContext = GetBindingContext(typeof(string));
bindingContext.ValueProvider = new SimpleHttpValueProvider bindingContext.ValueProvider = new SimpleHttpValueProvider
{ {
{ "theModelName", "" } { "theModelName", "" }
}; };
TypeConverterModelBinder binder = new TypeConverterModelBinder(); var binder = new TypeConverterModelBinder();
// Act // Act
bool retVal = await binder.BindModelAsync(bindingContext); var retVal = await binder.BindModelAsync(bindingContext);
// Assert // Assert
Assert.True(retVal); Assert.True(retVal);
@ -121,16 +123,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
public async Task BindModel_ValidValueProviderResult_ReturnsModel() public async Task BindModel_ValidValueProviderResult_ReturnsModel()
{ {
// Arrange // Arrange
ModelBindingContext bindingContext = GetBindingContext(typeof(int)); var bindingContext = GetBindingContext(typeof(int));
bindingContext.ValueProvider = new SimpleHttpValueProvider bindingContext.ValueProvider = new SimpleHttpValueProvider
{ {
{ "theModelName", "42" } { "theModelName", "42" }
}; };
TypeConverterModelBinder binder = new TypeConverterModelBinder(); var binder = new TypeConverterModelBinder();
// Act // Act
bool retVal = await binder.BindModelAsync(bindingContext); var retVal = await binder.BindModelAsync(bindingContext);
// Assert // Assert
Assert.True(retVal); Assert.True(retVal);

View File

@ -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);
}
}
}

View File

@ -71,6 +71,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
[Fact] [Fact]
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride()
{ {
if (TestPlatformHelper.IsMono)
{
// ValidationAttribute in Mono does not read non-public resx properties.
return;
}
// Arrange // Arrange
var metadataProvider = new DataAnnotationsModelMetadataProvider(); var metadataProvider = new DataAnnotationsModelMetadataProvider();
var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty");

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Equal("maxlength", rule.ValidationType); Assert.Equal("maxlength", rule.ValidationType);
Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(1, rule.ValidationParameters.Count);
Assert.Equal(10, rule.ValidationParameters["max"]); 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] [Fact]

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Equal("minlength", rule.ValidationType); Assert.Equal("minlength", rule.ValidationType);
Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(1, rule.ValidationParameters.Count);
Assert.Equal(6, rule.ValidationParameters["min"]); 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] [Fact]

View File

@ -305,7 +305,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Assert // Assert
Assert.Equal(3, context.ModelState.Count); Assert.Equal(3, context.ModelState.Count);
Assert.IsType<TooManyModelErrorsException>(context.ModelState[""].Errors[0].Exception); Assert.IsType<TooManyModelErrorsException>(context.ModelState[""].Errors[0].Exception);
Assert.Equal("The RequiredString field is required.", Assert.Equal(ValidationAttributeUtil.GetRequiredErrorMessage("RequiredString"),
context.ModelState["theKey.RequiredString"].Errors[0].ErrorMessage); context.ModelState["theKey.RequiredString"].Errors[0].ErrorMessage);
Assert.False(context.ModelState.ContainsKey("theKey.RangedInt")); Assert.False(context.ModelState.ContainsKey("theKey.RangedInt"));
Assert.False(context.ModelState.ContainsKey("theKey.ValidString")); Assert.False(context.ModelState.ContainsKey("theKey.ValidString"));

View File

@ -14,6 +14,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public void GetClientValidationRules_ReturnsValidationParameters() public void GetClientValidationRules_ReturnsValidationParameters()
{ {
// Arrange // Arrange
var expected = ValidationAttributeUtil.GetRequiredErrorMessage("Length");
var provider = new DataAnnotationsModelMetadataProvider(); var provider = new DataAnnotationsModelMetadataProvider();
var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length"); var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length");
var attribute = new RequiredAttribute(); var attribute = new RequiredAttribute();
@ -27,7 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
var rule = Assert.Single(rules); var rule = Assert.Single(rules);
Assert.Equal("required", rule.ValidationType); Assert.Equal("required", rule.ValidationType);
Assert.Empty(rule.ValidationParameters); Assert.Empty(rule.ValidationParameters);
Assert.Equal("The Length field is required.", rule.ErrorMessage); Assert.Equal(expected, rule.ErrorMessage);
} }
} }
} }

View File

@ -28,8 +28,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Equal("length", rule.ValidationType); Assert.Equal("length", rule.ValidationType);
Assert.Equal(1, rule.ValidationParameters.Count); Assert.Equal(1, rule.ValidationParameters.Count);
Assert.Equal(8, rule.ValidationParameters["max"]); Assert.Equal(8, rule.ValidationParameters["max"]);
Assert.Equal("The field Length must be a string with a maximum length of 8.", Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage);
rule.ErrorMessage);
} }
[Fact] [Fact]
@ -52,8 +51,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Equal(2, rule.ValidationParameters.Count); Assert.Equal(2, rule.ValidationParameters.Count);
Assert.Equal(3, rule.ValidationParameters["min"]); Assert.Equal(3, rule.ValidationParameters["min"]);
Assert.Equal(10, rule.ValidationParameters["max"]); 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.", Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage);
rule.ErrorMessage);
} }
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"compilationOptions": { "compilationOptions": {
"warningsAsErrors": true "warningsAsErrors": "true"
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNet.Http": "1.0.0-*", "Microsoft.AspNet.Http": "1.0.0-*",