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.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]

View File

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

View File

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

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]
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");

View File

@ -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]

View File

@ -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]

View File

@ -305,7 +305,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// Assert
Assert.Equal(3, context.ModelState.Count);
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);
Assert.False(context.ModelState.ContainsKey("theKey.RangedInt"));
Assert.False(context.ModelState.ContainsKey("theKey.ValidString"));

View File

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

View File

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

View File

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