Adding unit tests for CompositeModelBinder
This commit is contained in:
parent
527394c492
commit
ea3e958563
|
|
@ -3,6 +3,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.AspNet.DependencyInjection;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -52,7 +53,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(isBound);
|
Assert.True(isBound);
|
||||||
Assert.Equal(42, bindingContext.Model);
|
Assert.Equal(42, bindingContext.Model);
|
||||||
|
|
||||||
Assert.True(validationCalled);
|
Assert.True(validationCalled);
|
||||||
Assert.Equal(true, bindingContext.ModelState.IsValid);
|
Assert.Equal(true, bindingContext.ModelState.IsValid);
|
||||||
}
|
}
|
||||||
|
|
@ -158,12 +159,118 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
|
||||||
Assert.Null(bindingContext.Model);
|
Assert.Null(bindingContext.Model);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SimpleModel
|
[Fact]
|
||||||
|
public void BindModel_WithDefaultBinders_BindsSimpleType()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var binder = CreateBinderWithDefaults();
|
||||||
|
|
||||||
|
var valueProvider = new SimpleValueProvider
|
||||||
|
{
|
||||||
|
{ "firstName", "firstName-value"},
|
||||||
|
{ "lastName", "lastName-value"}
|
||||||
|
};
|
||||||
|
var bindingContext = CreateBindingContext(binder, valueProvider, typeof(SimplePropertiesModel));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var isBound = binder.BindModel(bindingContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.True(isBound);
|
||||||
|
var model = Assert.IsType<SimplePropertiesModel>(bindingContext.Model);
|
||||||
|
Assert.Equal("firstName-value", model.FirstName);
|
||||||
|
Assert.Equal("lastName-value", model.LastName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BindModel_WithDefaultBinders_BindsComplexType()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var binder = CreateBinderWithDefaults();
|
||||||
|
|
||||||
|
var valueProvider = new SimpleValueProvider
|
||||||
|
{
|
||||||
|
{ "firstName", "firstName-value"},
|
||||||
|
{ "lastName", "lastName-value"},
|
||||||
|
{ "friends[0].firstName", "first-friend"},
|
||||||
|
{ "friends[0].age", "40"},
|
||||||
|
{ "friends[0].friends[0].firstname", "nested friend"},
|
||||||
|
{ "friends[1].firstName", "some other"},
|
||||||
|
{ "friends[1].lastName", "name"},
|
||||||
|
};
|
||||||
|
var bindingContext = CreateBindingContext(binder, valueProvider, typeof(Person));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var isBound = binder.BindModel(bindingContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.True(isBound);
|
||||||
|
var model = Assert.IsType<Person>(bindingContext.Model);
|
||||||
|
Assert.Equal("firstName-value", model.FirstName);
|
||||||
|
Assert.Equal("lastName-value", model.LastName);
|
||||||
|
Assert.Equal(2, model.Friends.Count);
|
||||||
|
Assert.Equal("first-friend", model.Friends[0].FirstName);
|
||||||
|
Assert.Equal(40, model.Friends[0].Age);
|
||||||
|
var nestedFriend = Assert.Single(model.Friends[0].Friends);
|
||||||
|
Assert.Equal("nested friend", nestedFriend.FirstName);
|
||||||
|
Assert.Equal("some other", model.Friends[1].FirstName);
|
||||||
|
Assert.Equal("name", model.Friends[1].LastName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ModelBindingContext CreateBindingContext(IModelBinder binder,
|
||||||
|
IValueProvider valueProvider,
|
||||||
|
Type type)
|
||||||
|
{
|
||||||
|
var metadataProvider = new DataAnnotationsModelMetadataProvider();
|
||||||
|
var bindingContext = new ModelBindingContext
|
||||||
|
{
|
||||||
|
ModelBinder = binder,
|
||||||
|
FallbackToEmptyPrefix = true,
|
||||||
|
MetadataProvider = metadataProvider,
|
||||||
|
ModelMetadata = metadataProvider.GetMetadataForType(null, type),
|
||||||
|
ModelState = new ModelStateDictionary(),
|
||||||
|
ValueProvider = valueProvider,
|
||||||
|
ValidatorProviders = Enumerable.Empty<IModelValidatorProvider>()
|
||||||
|
};
|
||||||
|
return bindingContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static CompositeModelBinder CreateBinderWithDefaults()
|
||||||
|
{
|
||||||
|
var serviceProvider = Mock.Of<IServiceProvider>();
|
||||||
|
var typeActivator = new Mock<ITypeActivator>();
|
||||||
|
typeActivator
|
||||||
|
.Setup(t => t.CreateInstance(serviceProvider, It.IsAny<Type>(), It.IsAny<object[]>()))
|
||||||
|
.Returns((IServiceProvider sp, Type t, object[] args) => Activator.CreateInstance(t));
|
||||||
|
var binders = new IModelBinder[]
|
||||||
|
{
|
||||||
|
new TypeMatchModelBinder(),
|
||||||
|
new GenericModelBinder(serviceProvider, typeActivator.Object),
|
||||||
|
new ComplexModelDtoModelBinder(),
|
||||||
|
new TypeConverterModelBinder(),
|
||||||
|
new MutableObjectModelBinder()
|
||||||
|
};
|
||||||
|
var binder = new CompositeModelBinder(binders);
|
||||||
|
return binder;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SimplePropertiesModel
|
||||||
{
|
{
|
||||||
public string FirstName { get; set; }
|
public string FirstName { get; set; }
|
||||||
public string LastName { get; set; }
|
public string LastName { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private sealed class Person
|
||||||
|
{
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
|
||||||
|
public string LastName { get; set; }
|
||||||
|
|
||||||
|
public int Age { get; set; }
|
||||||
|
|
||||||
|
public List<Person> Friends { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
private class SimpleValueProvider : Dictionary<string, object>, IValueProvider
|
private class SimpleValueProvider : Dictionary<string, object>, IValueProvider
|
||||||
{
|
{
|
||||||
private readonly CultureInfo _culture;
|
private readonly CultureInfo _culture;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
"version" : "0.1-alpha-*",
|
"version" : "0.1-alpha-*",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
|
||||||
|
"Microsoft.AspNet.DependencyInjection": "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.PipelineCore": "0.1-alpha-*",
|
"Microsoft.AspNet.PipelineCore": "0.1-alpha-*",
|
||||||
"Microsoft.AspNet.Testing": "0.1-alpha-*",
|
"Microsoft.AspNet.Testing": "0.1-alpha-*",
|
||||||
"Microsoft.ComponentModel.DataAnnotations" : "4.0.10.0",
|
"Microsoft.ComponentModel.DataAnnotations" : "4.0.10.0",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue