1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Globalization;
using System.Web.Routing;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Test
{
public class ExpiicitRouteDataValueProviderFactoryTest
{
[Fact]
public void GetValueProviderReturnsChildActionValue()
{
// Arrange
ChildActionValueProviderFactory factory = new ChildActionValueProviderFactory();
ControllerContext controllerContext = new ControllerContext();
controllerContext.RouteData = new RouteData();
string conflictingKey = "conflictingKey";
controllerContext.RouteData.Values["conflictingKey"] = 43;
DictionaryValueProvider<object> explictValueDictionary = new DictionaryValueProvider<object>(new RouteValueDictionary { { conflictingKey, 42 } }, CultureInfo.InvariantCulture);
controllerContext.RouteData.Values[ChildActionValueProvider.ChildActionValuesKey] = explictValueDictionary;
// Act
IValueProvider valueProvider = factory.GetValueProvider(controllerContext);
// Assert
Assert.Equal(typeof(ChildActionValueProvider), valueProvider.GetType());
ValueProviderResult vpResult = valueProvider.GetValue(conflictingKey);
Assert.NotNull(vpResult);
Assert.Equal(42, vpResult.RawValue);
Assert.Equal("42", vpResult.AttemptedValue);
Assert.Equal(CultureInfo.InvariantCulture, vpResult.Culture);
}
[Fact]
public void GetValueProviderReturnsNullIfNoChildActionDictionary()
{
// Arrange
ChildActionValueProviderFactory factory = new ChildActionValueProviderFactory();
ControllerContext controllerContext = new ControllerContext();
controllerContext.RouteData = new RouteData();
controllerContext.RouteData.Values["forty-two"] = 42;
// Act
IValueProvider valueProvider = factory.GetValueProvider(controllerContext);
// Assert
Assert.Equal(typeof(ChildActionValueProvider), valueProvider.GetType());
ValueProviderResult vpResult = valueProvider.GetValue("forty-two");
Assert.Null(vpResult);
}
[Fact]
public void GetValueProviderReturnsNullIfKeyIsNotInChildActionDictionary()
{
// Arrange
ChildActionValueProviderFactory factory = new ChildActionValueProviderFactory();
ControllerContext controllerContext = new ControllerContext();
controllerContext.RouteData = new RouteData();
controllerContext.RouteData.Values["forty-two"] = 42;
DictionaryValueProvider<object> explictValueDictionary = new DictionaryValueProvider<object>(new RouteValueDictionary { { "forty-three", 42 } }, CultureInfo.CurrentUICulture);
controllerContext.RouteData.Values[ChildActionValueProvider.ChildActionValuesKey] = explictValueDictionary;
// Act
IValueProvider valueProvider = factory.GetValueProvider(controllerContext);
// Assert
Assert.Equal(typeof(ChildActionValueProvider), valueProvider.GetType());
ValueProviderResult vpResult = valueProvider.GetValue("forty-two");
Assert.Null(vpResult);
}
[Fact]
public void GetValueProvider_ThrowsIfControllerContextIsNull()
{
// Arrange
RouteDataValueProviderFactory factory = new RouteDataValueProviderFactory();
// Act & assert
Assert.ThrowsArgumentNull(
delegate { factory.GetValueProvider(null); }, "controllerContext");
}
}
}
|