public void GetBinder_TypeMatches_Success_Factory_ReturnsBinder() { // Arrange IExtensibleModelBinder binderInstance = new Mock <IExtensibleModelBinder>().Object; Func <Type[], IExtensibleModelBinder> binderFactory = typeArguments => { Assert.Equal(new[] { typeof(int) }, typeArguments); return(binderInstance); }; GenericModelBinderProvider provider = new GenericModelBinderProvider( typeof(IList <>), binderFactory ) { SuppressPrefixCheck = true }; ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List <int>)); // Act IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext); // Assert Assert.Same(binderInstance, returnedBinder); }
public void GetBinderThrowsIfBindingContextIsNull() { // Arrange GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IEnumerable <>), typeof(CollectionModelBinder <>)); // Act & assert Assert.ThrowsArgumentNull( delegate { provider.GetBinder(null, null); }, "bindingContext"); }
public void GetBinder_TypeDoesNotMatch_ModelTypeIsNotInterface_ReturnsNull() { // Arrange GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), typeof(CollectionModelBinder <>)) { SuppressPrefixCheck = true }; ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(object)); // Act IExtensibleModelBinder binder = provider.GetBinder(null, bindingContext); // Assert Assert.Null(binder); }
public void GetBinder_TypeMatches_Success_TypeActivation_ReturnsBinder() { // Arrange GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), typeof(CollectionModelBinder <>)) { SuppressPrefixCheck = true }; ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List <int>)); // Act IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext); // Assert Assert.IsType <CollectionModelBinder <int> >(returnedBinder); }
public void GetBinder_TypeMatches_PrefixNotFound_ReturnsNull() { // Arrange IExtensibleModelBinder binderInstance = new Mock <IExtensibleModelBinder>().Object; GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), binderInstance); ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List <int>)); bindingContext.ValueProvider = new SimpleValueProvider(); // Act IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext); // Assert Assert.Null(returnedBinder); }
public void GetBinderThrowsIfModelBinderHasNoParameterlessConstructor() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List <int>)); GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), typeof(NoParameterlessCtorBinder <>)) { SuppressPrefixCheck = true, }; // Act & Assert, confirming type name and full stack are available in Exception MissingMethodException exception = Assert.Throws <MissingMethodException>( () => provider.GetBinder(null, bindingContext), "No parameterless constructor defined for this object. Object type 'Microsoft.Web.Mvc.ModelBinding.Test.GenericModelBinderProviderTest+NoParameterlessCtorBinder`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'."); Assert.Contains("System.Activator.CreateInstance(", exception.ToString()); }
public void GetBinder_TypeMatches_Success_Instance_ReturnsBinder() { // Arrange IExtensibleModelBinder binderInstance = new Mock <IExtensibleModelBinder>().Object; GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), binderInstance) { SuppressPrefixCheck = true }; ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List <int>)); // Act IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext); // Assert Assert.Same(binderInstance, returnedBinder); }
public void RegisterBinderForGenericType_Instance() { // Arrange ModelBinderProvider mockProvider = new Mock <ModelBinderProvider>().Object; IExtensibleModelBinder mockBinder = new Mock <IExtensibleModelBinder>().Object; ModelBinderProviderCollection collection = new ModelBinderProviderCollection() { mockProvider }; // Act collection.RegisterBinderForGenericType(typeof(List <>), mockBinder); // Assert GenericModelBinderProvider genericProvider = collection[0] as GenericModelBinderProvider; Assert.IsNotNull(genericProvider, "Generic provider should've been inserted at the front of the list."); Assert.AreEqual(typeof(List <>), genericProvider.ModelType); Assert.AreEqual(mockProvider, collection[1]); }
public void GetBinderThrowsIfBindingContextIsNull() { // Arrange GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IEnumerable<>), typeof(CollectionModelBinder<>)); // Act & assert ExceptionHelper.ExpectArgumentNullException( delegate { provider.GetBinder(null, null); }, "bindingContext"); }
public void GetBinder_TypeMatches_Success_TypeActivation_ReturnsBinder() { // Arrange GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), typeof(CollectionModelBinder<>)) { SuppressPrefixCheck = true }; ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List<int>)); // Act IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext); // Assert Assert.IsInstanceOfType(returnedBinder, typeof(CollectionModelBinder<int>)); }
public void GetBinder_TypeMatches_Success_Instance_ReturnsBinder() { // Arrange IExtensibleModelBinder binderInstance = new Mock<IExtensibleModelBinder>().Object; GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), binderInstance) { SuppressPrefixCheck = true }; ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List<int>)); // Act IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext); // Assert Assert.AreSame(binderInstance, returnedBinder); }
public void GetBinder_TypeMatches_Success_Factory_ReturnsBinder() { // Arrange IExtensibleModelBinder binderInstance = new Mock<IExtensibleModelBinder>().Object; Func<Type[], IExtensibleModelBinder> binderFactory = typeArguments => { CollectionAssert.AreEqual(new Type[] { typeof(int) }, typeArguments); return binderInstance; }; GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IList<>), binderFactory) { SuppressPrefixCheck = true }; ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List<int>)); // Act IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext); // Assert Assert.AreSame(binderInstance, returnedBinder); }
public void GetBinder_TypeMatches_PrefixNotFound_ReturnsNull() { // Arrange IExtensibleModelBinder binderInstance = new Mock<IExtensibleModelBinder>().Object; GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), binderInstance); ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List<int>)); bindingContext.ValueProvider = new SimpleValueProvider(); // Act IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext); // Assert Assert.IsNull(returnedBinder); }
public void GetBinder_TypeDoesNotMatch_ModelTypeIsNotInterface_ReturnsNull() { // Arrange GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), typeof(CollectionModelBinder<>)) { SuppressPrefixCheck = true }; ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(object)); // Act IExtensibleModelBinder binder = provider.GetBinder(null, bindingContext); // Assert Assert.IsNull(binder); }