Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 3cd5c17

Browse files
committed
Updating ModelBinding tests to work in Mono
1 parent 497274a commit 3cd5c17

11 files changed

+68
-37
lines changed

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/ByteArrayModelBinderTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Linq;
77
using System.Threading.Tasks;
8+
using Microsoft.AspNet.Testing;
89
using Xunit;
910

1011
namespace Microsoft.AspNet.Mvc.ModelBinding.Test
@@ -58,6 +59,11 @@ public async Task BindModel()
5859
public async Task BindModelAddsModelErrorsOnInvalidCharacters()
5960
{
6061
// Arrange
62+
var expected = TestPlatformHelper.IsMono ?
63+
"Invalid length." :
64+
"The input is not a valid Base-64 string as it contains a non-base 64 character," +
65+
" more than two padding characters, or an illegal character among the padding characters. ";
66+
6167
var valueProvider = new SimpleHttpValueProvider()
6268
{
6369
{ "foo", "\"Fys1\"" }
@@ -72,10 +78,8 @@ public async Task BindModelAddsModelErrorsOnInvalidCharacters()
7278
// Assert
7379
Assert.True(binderResult);
7480
Assert.False(bindingContext.ModelState.IsValid);
75-
Assert.Equal(1, bindingContext.ModelState.Values.Count);
76-
Assert.Equal("The input is not a valid Base-64 string as it contains a non-base 64 character," +
77-
" more than two padding characters, or an illegal character among the padding characters. ",
78-
bindingContext.ModelState.Values.First().Errors[0].ErrorMessage);
81+
var error = Assert.Single(bindingContext.ModelState["foo"].Errors);
82+
Assert.Equal(expected, error.ErrorMessage);
7983
}
8084

8185
[Fact]

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/MutableObjectModelBinderTest.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ public void EnsureModel_ModelIsNull_CallsCreateModel()
176176
.Returns(new Person()).Verifiable();
177177

178178
// Act
179-
object originalModel = bindingContext.Model;
179+
var originalModel = bindingContext.Model;
180180
testableBinder.Object.EnsureModelPublic(bindingContext);
181-
object newModel = bindingContext.Model;
181+
var newModel = bindingContext.Model;
182182

183183
// Assert
184184
Assert.Null(originalModel);
@@ -430,7 +430,8 @@ public void ProcessDto_RequiredFieldMissing_RaisesModelError()
430430
var modelError = modelState.Errors[0];
431431
Assert.Null(modelError.Exception);
432432
Assert.NotNull(modelError.ErrorMessage);
433-
Assert.Equal("The Age field is required.", modelError.ErrorMessage);
433+
var expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.Age));
434+
Assert.Equal(expected, modelError.ErrorMessage);
434435

435436
// Check City error.
436437
Assert.True(modelStateDictionary.TryGetValue("theModel.City", out modelState));
@@ -439,7 +440,8 @@ public void ProcessDto_RequiredFieldMissing_RaisesModelError()
439440
modelError = modelState.Errors[0];
440441
Assert.Null(modelError.Exception);
441442
Assert.NotNull(modelError.ErrorMessage);
442-
Assert.Equal("The City field is required.", modelError.ErrorMessage);
443+
expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.City));
444+
Assert.Equal(expected, modelError.ErrorMessage);
443445
}
444446

445447
[Fact]
@@ -478,7 +480,8 @@ public void ProcessDto_RequiredFieldNull_RaisesModelError()
478480
var modelError = modelState.Errors[0];
479481
Assert.Null(modelError.Exception);
480482
Assert.NotNull(modelError.ErrorMessage);
481-
Assert.Equal("The City field is required.", modelError.ErrorMessage);
483+
var expected = ValidationAttributeUtil.GetRequiredErrorMessage(nameof(ModelWithRequired.City));
484+
Assert.Equal(expected, modelError.ErrorMessage);
482485
}
483486

484487
[Fact]
@@ -521,8 +524,8 @@ public void ProcessDto_RequiredFieldNull_RaisesModelErrorWithMessage()
521524

522525
var bindingContext = CreateContext(containerMetadata);
523526

524-
ComplexModelDto dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties);
525-
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
527+
var dto = new ComplexModelDto(containerMetadata, containerMetadata.Properties);
528+
var testableBinder = new TestableMutableObjectModelBinder();
526529

527530
// Make ValueTypeRequired invalid.
528531
var propertyMetadata = dto.PropertyMetadata.Single(p => p.PropertyName == "ValueTypeRequired");
@@ -533,7 +536,7 @@ public void ProcessDto_RequiredFieldNull_RaisesModelErrorWithMessage()
533536
testableBinder.ProcessDto(bindingContext, dto);
534537

535538
// Assert
536-
ModelStateDictionary modelStateDictionary = bindingContext.ModelState;
539+
var modelStateDictionary = bindingContext.ModelState;
537540
Assert.Equal(false, modelStateDictionary.IsValid);
538541
Assert.Equal(1, modelStateDictionary.Count);
539542

@@ -542,7 +545,7 @@ public void ProcessDto_RequiredFieldNull_RaisesModelErrorWithMessage()
542545
Assert.True(modelStateDictionary.TryGetValue("theModel.ValueTypeRequired", out modelState));
543546
Assert.Equal(1, modelState.Errors.Count);
544547

545-
ModelError modelError = modelState.Errors[0];
548+
var modelError = modelState.Errors[0];
546549
Assert.Null(modelError.Exception);
547550
Assert.NotNull(modelError.ErrorMessage);
548551
Assert.Equal("Sample message", modelError.ErrorMessage);

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Binders/TypeConverterModelBinderTest.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.ComponentModel;
65
using System.Globalization;
76
using System.Threading.Tasks;
7+
using Microsoft.AspNet.Testing;
88
using Xunit;
99

1010
namespace Microsoft.AspNet.Mvc.ModelBinding.Test
@@ -62,34 +62,36 @@ public async Task BindModel_ReturnsTrue_IfTypeCanBeConverted(Type destinationTyp
6262
public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState()
6363
{
6464
// Arrange
65-
ModelBindingContext bindingContext = GetBindingContext(typeof(int));
65+
var message = TestPlatformHelper.IsMono ? "Input string was not in the correct format" :
66+
"Input string was not in a correct format.";
67+
var bindingContext = GetBindingContext(typeof(int));
6668
bindingContext.ValueProvider = new SimpleHttpValueProvider
6769
{
6870
{ "theModelName", "not an integer" }
6971
};
7072

71-
TypeConverterModelBinder binder = new TypeConverterModelBinder();
73+
var binder = new TypeConverterModelBinder();
7274

7375
// Act
74-
bool retVal = await binder.BindModelAsync(bindingContext);
76+
var retVal = await binder.BindModelAsync(bindingContext);
7577

7678
// Assert
7779
Assert.True(retVal);
7880
Assert.Null(bindingContext.Model);
7981
Assert.Equal(false, bindingContext.ModelState.IsValid);
80-
Assert.Equal("Input string was not in a correct format.", bindingContext.ModelState["theModelName"].Errors[0].ErrorMessage);
82+
var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors);
83+
Assert.Equal(message, error.ErrorMessage);
8184
}
8285

8386
[Fact]
8487
public async Task BindModel_NullValueProviderResult_ReturnsFalse()
8588
{
8689
// Arrange
87-
ModelBindingContext bindingContext = GetBindingContext(typeof(int));
88-
89-
TypeConverterModelBinder binder = new TypeConverterModelBinder();
90+
var bindingContext = GetBindingContext(typeof(int));
91+
var binder = new TypeConverterModelBinder();
9092

9193
// Act
92-
bool retVal = await binder.BindModelAsync(bindingContext);
94+
var retVal = await binder.BindModelAsync(bindingContext);
9395

9496
// Assert
9597
Assert.False(retVal, "BindModel should have returned null.");
@@ -100,16 +102,16 @@ public async Task BindModel_NullValueProviderResult_ReturnsFalse()
100102
public async Task BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull()
101103
{
102104
// Arrange
103-
ModelBindingContext bindingContext = GetBindingContext(typeof(string));
105+
var bindingContext = GetBindingContext(typeof(string));
104106
bindingContext.ValueProvider = new SimpleHttpValueProvider
105107
{
106108
{ "theModelName", "" }
107109
};
108110

109-
TypeConverterModelBinder binder = new TypeConverterModelBinder();
111+
var binder = new TypeConverterModelBinder();
110112

111113
// Act
112-
bool retVal = await binder.BindModelAsync(bindingContext);
114+
var retVal = await binder.BindModelAsync(bindingContext);
113115

114116
// Assert
115117
Assert.True(retVal);
@@ -121,16 +123,16 @@ public async Task BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull()
121123
public async Task BindModel_ValidValueProviderResult_ReturnsModel()
122124
{
123125
// Arrange
124-
ModelBindingContext bindingContext = GetBindingContext(typeof(int));
126+
var bindingContext = GetBindingContext(typeof(int));
125127
bindingContext.ValueProvider = new SimpleHttpValueProvider
126128
{
127129
{ "theModelName", "42" }
128130
};
129131

130-
TypeConverterModelBinder binder = new TypeConverterModelBinder();
132+
var binder = new TypeConverterModelBinder();
131133

132134
// Act
133-
bool retVal = await binder.BindModelAsync(bindingContext);
135+
var retVal = await binder.BindModelAsync(bindingContext);
134136

135137
// Assert
136138
Assert.True(retVal);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.ComponentModel.DataAnnotations;
6+
7+
namespace Microsoft.AspNet.Mvc.ModelBinding
8+
{
9+
public static class ValidationAttributeUtil
10+
{
11+
public static string GetRequiredErrorMessage(string field)
12+
{
13+
var attr = new RequiredAttribute();
14+
return attr.FormatErrorMessage(field);
15+
}
16+
}
17+
}

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/CompareAttributeTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public void ClientRulesWithCompareAttribute_ErrorMessageUsesOverride()
7171
[Fact]
7272
public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride()
7373
{
74+
if (TestPlatformHelper.IsMono)
75+
{
76+
// ValidationAttribute in Mono does not read non-public resx properties.
77+
return;
78+
}
79+
7480
// Arrange
7581
var metadataProvider = new DataAnnotationsModelMetadataProvider();
7682
var metadata = metadataProvider.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty");

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MaxLengthAttributeAdapterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void ClientRulesWithMaxLengthAttribute()
2828
Assert.Equal("maxlength", rule.ValidationType);
2929
Assert.Equal(1, rule.ValidationParameters.Count);
3030
Assert.Equal(10, rule.ValidationParameters["max"]);
31-
Assert.Equal("The field Length must be a string or array type with a maximum length of '10'.", rule.ErrorMessage);
31+
Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage);
3232
}
3333

3434
[Fact]

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/MinLengthAttributeAdapterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void ClientRulesWithMinLengthAttribute()
2828
Assert.Equal("minlength", rule.ValidationType);
2929
Assert.Equal(1, rule.ValidationParameters.Count);
3030
Assert.Equal(6, rule.ValidationParameters["min"]);
31-
Assert.Equal("The field Length must be a string or array type with a minimum length of '6'.", rule.ErrorMessage);
31+
Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage);
3232
}
3333

3434
[Fact]

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/ModelValidationNodeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void Validate_ShortCircuits_IfModelStateHasReachedMaxNumberOfErrors()
305305
// Assert
306306
Assert.Equal(3, context.ModelState.Count);
307307
Assert.IsType<TooManyModelErrorsException>(context.ModelState[""].Errors[0].Exception);
308-
Assert.Equal("The RequiredString field is required.",
308+
Assert.Equal(ValidationAttributeUtil.GetRequiredErrorMessage("RequiredString"),
309309
context.ModelState["theKey.RequiredString"].Errors[0].ErrorMessage);
310310
Assert.False(context.ModelState.ContainsKey("theKey.RangedInt"));
311311
Assert.False(context.ModelState.ContainsKey("theKey.ValidString"));

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/RequiredAttributeAdapterTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class RequiredAttributeAdapterTest
1414
public void GetClientValidationRules_ReturnsValidationParameters()
1515
{
1616
// Arrange
17+
var expected = ValidationAttributeUtil.GetRequiredErrorMessage("Length");
1718
var provider = new DataAnnotationsModelMetadataProvider();
1819
var metadata = provider.GetMetadataForProperty(() => null, typeof(string), "Length");
1920
var attribute = new RequiredAttribute();
@@ -27,7 +28,7 @@ public void GetClientValidationRules_ReturnsValidationParameters()
2728
var rule = Assert.Single(rules);
2829
Assert.Equal("required", rule.ValidationType);
2930
Assert.Empty(rule.ValidationParameters);
30-
Assert.Equal("The Length field is required.", rule.ErrorMessage);
31+
Assert.Equal(expected, rule.ErrorMessage);
3132
}
3233
}
3334
}

test/Microsoft.AspNet.Mvc.ModelBinding.Test/Validation/StringLengthAttributeAdapterTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public void GetClientValidationRules_WithMaxLength_ReturnsValidationParameters()
2828
Assert.Equal("length", rule.ValidationType);
2929
Assert.Equal(1, rule.ValidationParameters.Count);
3030
Assert.Equal(8, rule.ValidationParameters["max"]);
31-
Assert.Equal("The field Length must be a string with a maximum length of 8.",
32-
rule.ErrorMessage);
31+
Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage);
3332
}
3433

3534
[Fact]
@@ -52,8 +51,7 @@ public void GetClientValidationRules_WithMinAndMaxLength_ReturnsValidationParame
5251
Assert.Equal(2, rule.ValidationParameters.Count);
5352
Assert.Equal(3, rule.ValidationParameters["min"]);
5453
Assert.Equal(10, rule.ValidationParameters["max"]);
55-
Assert.Equal("The field Length must be a string with a minimum length of 3 and a maximum length of 10.",
56-
rule.ErrorMessage);
54+
Assert.Equal(attribute.FormatErrorMessage("Length"), rule.ErrorMessage);
5755
}
5856
}
5957
}

0 commit comments

Comments
 (0)