Ease unit testing of `Controller.TryValidateModel()`
- #3586 - reverse conditions to access `_options.Value` only when test has set up an `IStringLocalizerFactory`
This commit is contained in:
parent
2dc13b523d
commit
62978229c4
|
|
@ -2,7 +2,9 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
#if DOTNET5_4
|
||||
using System.Reflection;
|
||||
#endif
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.OptionsModel;
|
||||
|
||||
|
|
@ -34,7 +36,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
|
|||
public void GetValidators(ModelValidatorProviderContext context)
|
||||
{
|
||||
IStringLocalizer stringLocalizer = null;
|
||||
if (_options.Value.DataAnnotationLocalizerProvider != null && _stringLocalizerFactory != null)
|
||||
if (_stringLocalizerFactory != null && _options.Value.DataAnnotationLocalizerProvider != null)
|
||||
{
|
||||
stringLocalizer = _options.Value.DataAnnotationLocalizerProvider(
|
||||
context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType,
|
||||
|
|
|
|||
|
|
@ -1770,6 +1770,22 @@ namespace Microsoft.AspNet.Mvc.Test
|
|||
Assert.Equal("Out of range!", error.ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryValidateModel_Succeeds_WithoutValidatorMetadata()
|
||||
{
|
||||
// Arrange
|
||||
// Do not add a Mock validator provider to this test. Test is intended to demonstrate ease of unit testing
|
||||
// and exercise DataAnnotationsModelValidatorProvider, avoiding #3586 regressions.
|
||||
var model = new TryValidateModelModel();
|
||||
var controller = GetController(binder: null, provider: null);
|
||||
|
||||
// Act
|
||||
var result = controller.TryValidateModel(model);
|
||||
|
||||
// Assert
|
||||
Assert.True(controller.ModelState.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryValidateModelEmptyBindingContextThrowsException()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue