103 lines
3.4 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Web.UnitTestUtil;
using Xunit;
namespace Microsoft.Web.Mvc.ModelBinding.Test
{
public class ArrayModelBinderProviderTest
{
[Fact]
public void GetBinder_CorrectModelTypeAndValueProviderEntries_ReturnsBinder()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])),
ModelName = "foo",
ValueProvider = new SimpleValueProvider
{
{ "foo[0]", "42" },
}
};
ArrayModelBinderProvider binderProvider = new ArrayModelBinderProvider();
// Act
IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext);
// Assert
Assert.IsType<ArrayModelBinder<int>>(binder);
}
[Fact]
public void GetBinder_ModelMetadataReturnsReadOnly_ReturnsNull()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])),
ModelName = "foo",
ValueProvider = new SimpleValueProvider
{
{ "foo[0]", "42" },
}
};
bindingContext.ModelMetadata.IsReadOnly = true;
ArrayModelBinderProvider binderProvider = new ArrayModelBinderProvider();
// Act
IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext);
// Assert
Assert.Null(binder);
}
[Fact]
public void GetBinder_ModelTypeIsIncorrect_ReturnsNull()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ICollection<int>)),
ModelName = "foo",
ValueProvider = new SimpleValueProvider
{
{ "foo[0]", "42" },
}
};
ArrayModelBinderProvider binderProvider = new ArrayModelBinderProvider();
// Act
IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext);
// Assert
Assert.Null(binder);
}
[Fact]
public void GetBinder_ValueProviderDoesNotContainPrefix_ReturnsNull()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])),
ModelName = "foo",
ValueProvider = new SimpleValueProvider()
};
ArrayModelBinderProvider binderProvider = new ArrayModelBinderProvider();
// Act
IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext);
// Assert
Assert.Null(binder);
}
}
}