public void GetBinderReturnsNullIfNoProviderMatches() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType( null, typeof(object) ) }; ModelBinderProviderCollection collection = new ModelBinderProviderCollection( new[] { new Mock <ModelBinderProvider>().Object, } ); // Act IExtensibleModelBinder returned = collection.GetBinder( controllerContext, bindingContext ); // Assert Assert.Null(returned); }
public void GetBinder_FromAttribute_Binder_ValueNotPresent_ReturnsNull() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType( null, typeof(ModelWithProviderAttribute_Binder) ), ModelName = "foo", ValueProvider = new SimpleValueProvider { { "bar", "barValue" } } }; ModelBinderProviderCollection providers = new ModelBinderProviderCollection(); providers.RegisterBinderForType( typeof(ModelWithProviderAttribute_Binder), new Mock <IExtensibleModelBinder>().Object, true /* suppressPrefix */ ); // Act IExtensibleModelBinder binder = providers.GetBinder(controllerContext, bindingContext); // Assert Assert.Null(binder); }
public void GetBinder_FromAttribute_Provider_ReturnsBinder() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType( null, typeof(ModelWithProviderAttribute_Provider) ) }; ModelBinderProviderCollection providers = new ModelBinderProviderCollection(); providers.RegisterBinderForType( typeof(ModelWithProviderAttribute_Provider), new Mock <IExtensibleModelBinder>().Object, true /* suppressPrefix */ ); // Act IExtensibleModelBinder binder = providers.GetBinder(controllerContext, bindingContext); // Assert Assert.IsType <CustomBinder>(binder); }
public void GetBinderReturnsValueFromFirstSuccessfulBinderProvider() { // Arrange var testType = typeof(string); IModelBinder nullModelBinder = null; IModelBinder expectedBinder = new Mock<IModelBinder>().Object; IModelBinder secondMatchingBinder = new Mock<IModelBinder>().Object; var provider1 = new Mock<IModelBinderProvider>(); provider1.Setup(p => p.GetBinder(testType)).Returns(nullModelBinder); var provider2 = new Mock<IModelBinderProvider>(MockBehavior.Strict); provider2.Setup(p => p.GetBinder(testType)).Returns(expectedBinder); var provider3 = new Mock<IModelBinderProvider>(MockBehavior.Strict); provider3.Setup(p => p.GetBinder(testType)).Returns(secondMatchingBinder); var collection = new ModelBinderProviderCollection(new[] { provider1.Object, provider2.Object, provider3.Object }); // Act IModelBinder returnedBinder = collection.GetBinder(testType); // Assert Assert.Same(expectedBinder, returnedBinder); }
public void GetBinderReturnsValueFromFirstSuccessfulBinderProvider() { // Arrange var testType = typeof(string); IModelBinder nullModelBinder = null; IModelBinder expectedBinder = new Mock <IModelBinder>().Object; IModelBinder secondMatchingBinder = new Mock <IModelBinder>().Object; var provider1 = new Mock <IModelBinderProvider>(); provider1.Setup(p => p.GetBinder(testType)).Returns(nullModelBinder); var provider2 = new Mock <IModelBinderProvider>(MockBehavior.Strict); provider2.Setup(p => p.GetBinder(testType)).Returns(expectedBinder); var provider3 = new Mock <IModelBinderProvider>(MockBehavior.Strict); provider3.Setup(p => p.GetBinder(testType)).Returns(secondMatchingBinder); var collection = new ModelBinderProviderCollection(new[] { provider1.Object, provider2.Object, provider3.Object }); // Act IModelBinder returnedBinder = collection.GetBinder(testType); // Assert Assert.Same(expectedBinder, returnedBinder); }
public void GetBinderDelegatesToResolver() { // Arrange Type modelType = typeof(string); IModelBinder expectedBinder = new Mock <IModelBinder>().Object; Mock <IModelBinderProvider> locatedProvider = new Mock <IModelBinderProvider>(); locatedProvider.Setup(p => p.GetBinder(modelType)) .Returns(expectedBinder); Mock <IModelBinderProvider> secondProvider = new Mock <IModelBinderProvider>(); Mock <IModelBinderProvider> thirdProvider = new Mock <IModelBinderProvider>(); IModelBinderProvider[] dependencyProviders = new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object }; IModelBinderProvider[] collectionProviders = new IModelBinderProvider[] { thirdProvider.Object }; Mock <IDependencyResolver> resolver = new Mock <IDependencyResolver>(); resolver.Setup(r => r.GetServices(typeof(IModelBinderProvider))).Returns(dependencyProviders); ModelBinderProviderCollection providers = new ModelBinderProviderCollection(collectionProviders, resolver.Object); // Act IModelBinder returnedBinder = providers.GetBinder(modelType); // Assert Assert.Same(expectedBinder, returnedBinder); }
public void GetBinderReturnsFirstBinderFromProviders() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(object)) }; IExtensibleModelBinder expectedBinder = new Mock <IExtensibleModelBinder>().Object; Mock <ModelBinderProvider> mockProvider = new Mock <ModelBinderProvider>(); mockProvider.Setup(p => p.GetBinder(controllerContext, bindingContext)).Returns(expectedBinder); ModelBinderProviderCollection collection = new ModelBinderProviderCollection(new[] { new Mock <ModelBinderProvider>().Object, mockProvider.Object, new Mock <ModelBinderProvider>().Object }); // Act IExtensibleModelBinder returned = collection.GetBinder(controllerContext, bindingContext); // Assert Assert.Equal(expectedBinder, returned); }
public void GuardClause() { // Arrange var collection = new ModelBinderProviderCollection(); // Act & Assert Assert.ThrowsArgumentNull(() => collection.GetBinder(null), "modelType"); }
public void GetBinderThrowsIfControllerContextIsNull() { // Arrange ModelBinderProviderCollection collection = new ModelBinderProviderCollection(); // Act & assert Assert.ThrowsArgumentNull( delegate { collection.GetBinder(null, new ExtensibleModelBindingContext()); }, "controllerContext"); }
public void GetBinderThrowsIfBindingContextIsNull() { // Arrange ModelBinderProviderCollection collection = new ModelBinderProviderCollection(); // Act & assert ExceptionHelper.ExpectArgumentNullException( delegate { collection.GetBinder(new ControllerContext(), null); }, "bindingContext"); }
public void GuardClause() { //Arrange var collection = new ModelBinderProviderCollection(); //Act & Assert ExceptionHelper.ExpectArgumentNullException( () => collection.GetBinder(null), "modelType" ); }
public void GuardClause() { // Arrange var collection = new ModelBinderProviderCollection(); // Act & Assert Assert.ThrowsArgumentNull( () => collection.GetBinder(null), "modelType" ); }
public void GetBinderUsesRegisteredProviders() { //Arrange var testType = typeof(string); var expectedBinder = new Mock<IModelBinder>().Object; var provider = new Mock<IModelBinderProvider>(MockBehavior.Strict); provider.Setup(p => p.GetBinder(testType)).Returns(expectedBinder); var collection = new ModelBinderProviderCollection(new[] { provider.Object }); //Act IModelBinder returnedBinder = collection.GetBinder(testType); //Assert Assert.AreSame(expectedBinder, returnedBinder); }
public void GetBinderThrowsIfModelTypeHasBindAttribute() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ModelWithBindAttribute)) }; ModelBinderProviderCollection collection = new ModelBinderProviderCollection(); // Act & assert Assert.Throws <InvalidOperationException>( delegate { collection.GetBinder(controllerContext, bindingContext); }, @"The model of type 'Microsoft.Web.Mvc.ModelBinding.Test.ModelBinderProviderCollectionTest+ModelWithBindAttribute' has a [Bind] attribute. The new model binding system cannot be used with models that have type-level [Bind] attributes. Use the [BindRequired] and [BindNever] attributes on the model type or its properties instead."); }
public void GetBinder_FromAttribute_BadAttribute_Throws() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ModelWithProviderAttribute_BadAttribute)) }; ModelBinderProviderCollection providers = new ModelBinderProviderCollection(); // Act & assert Assert.Throws <InvalidOperationException>( delegate { providers.GetBinder(controllerContext, bindingContext); }, @"The type 'System.Object' does not subclass Microsoft.Web.Mvc.ModelBinding.ModelBinderProvider or implement the interface Microsoft.Web.Mvc.ModelBinding.IExtensibleModelBinder."); }
public void GetBinderUsesRegisteredProviders() { // Arrange var testType = typeof(string); var expectedBinder = new Mock <IModelBinder>().Object; var provider = new Mock <IModelBinderProvider>(MockBehavior.Strict); provider.Setup(p => p.GetBinder(testType)).Returns(expectedBinder); var collection = new ModelBinderProviderCollection(new[] { provider.Object }); // Act IModelBinder returnedBinder = collection.GetBinder(testType); // Assert Assert.Same(expectedBinder, returnedBinder); }
public void GetBinderThrowsIfGenericProviderHasNoParameterlessConstructor() { // Arrange ControllerContext controllerContext = new ControllerContext(); ModelBinderProviderCollection collection = new ModelBinderProviderCollection(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType( null, typeof(ModelWithProviderAttribute_ProviderHasNoParameterlessConstructor <int>)), }; // Act & Assert, confirming type name and full stack are available in Exception MissingMethodException exception = Assert.Throws <MissingMethodException>( () => collection.GetBinder(controllerContext, bindingContext), "No parameterless constructor defined for this object. Object type 'Microsoft.Web.Mvc.ModelBinding.Test.ModelBinderProviderCollectionTest+NoParameterlessCtorBinder`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'."); Assert.Contains("System.Activator.CreateInstance(", exception.ToString()); }
public void GetBinderReturnsNullWhenNoSuccessfulBinderProviders() { //Arrange var testType = typeof(string); IModelBinder nullModelBinder = null; var provider1 = new Mock<IModelBinderProvider>(); provider1.Setup(p => p.GetBinder(testType)).Returns(nullModelBinder); var provider2 = new Mock<IModelBinderProvider>(MockBehavior.Strict); provider2.Setup(p => p.GetBinder(testType)).Returns(nullModelBinder); var collection = new ModelBinderProviderCollection(new[] { provider1.Object, provider2.Object }); //Act IModelBinder returnedBinder = collection.GetBinder(testType); //Assert Assert.IsNull(returnedBinder); }
public void GetBinderReturnsNullWhenNoSuccessfulBinderProviders() { // Arrange var testType = typeof(string); IModelBinder nullModelBinder = null; var provider1 = new Mock <IModelBinderProvider>(); provider1.Setup(p => p.GetBinder(testType)).Returns(nullModelBinder); var provider2 = new Mock <IModelBinderProvider>(MockBehavior.Strict); provider2.Setup(p => p.GetBinder(testType)).Returns(nullModelBinder); var collection = new ModelBinderProviderCollection(new[] { provider1.Object, provider2.Object }); // Act IModelBinder returnedBinder = collection.GetBinder(testType); // Assert Assert.Null(returnedBinder); }
public void GetBinder_FromAttribute_Binder_ValuePresent_ReturnsBinder() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ModelWithProviderAttribute_Binder)), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo", "fooValue" } } }; ModelBinderProviderCollection providers = new ModelBinderProviderCollection(); providers.RegisterBinderForType(typeof(ModelWithProviderAttribute_Binder), new Mock <IExtensibleModelBinder>().Object, true /* suppressPrefix */); // Act IExtensibleModelBinder binder = providers.GetBinder(controllerContext, bindingContext); // Assert Assert.AreEqual(typeof(CustomBinder), binder.GetType(), "Binder should've come from attribute rather than collection."); }
public void GetBinderDelegatesToResolver() { // Arrange Type modelType = typeof(string); IModelBinder expectedBinder = new Mock <IModelBinder>().Object; Mock <IModelBinderProvider> locatedProvider = new Mock <IModelBinderProvider>(); locatedProvider.Setup(p => p.GetBinder(modelType)) .Returns(expectedBinder); Mock <IModelBinderProvider> secondProvider = new Mock <IModelBinderProvider>(); Resolver <IEnumerable <IModelBinderProvider> > resolver = new Resolver <IEnumerable <IModelBinderProvider> > { Current = new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object } }; ModelBinderProviderCollection providers = new ModelBinderProviderCollection(resolver); // Act IModelBinder returnedBinder = providers.GetBinder(modelType); // Assert Assert.Same(expectedBinder, returnedBinder); }
public void GetBinderDelegatesToResolver() { // Arrange Type modelType = typeof(string); IModelBinder expectedBinder = new Mock<IModelBinder>().Object; Mock<IModelBinderProvider> locatedProvider = new Mock<IModelBinderProvider>(); locatedProvider.Setup(p => p.GetBinder(modelType)) .Returns(expectedBinder); Mock<IModelBinderProvider> secondProvider = new Mock<IModelBinderProvider>(); Resolver<IEnumerable<IModelBinderProvider>> resolver = new Resolver<IEnumerable<IModelBinderProvider>> { Current = new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object } }; ModelBinderProviderCollection providers = new ModelBinderProviderCollection(resolver); // Act IModelBinder returnedBinder = providers.GetBinder(modelType); // Assert Assert.Same(expectedBinder, returnedBinder); }
public void GetBinderDelegatesToResolver() { // Arrange Type modelType = typeof(string); IModelBinder expectedBinder = new Mock<IModelBinder>().Object; Mock<IModelBinderProvider> locatedProvider = new Mock<IModelBinderProvider>(); locatedProvider.Setup(p => p.GetBinder(modelType)) .Returns(expectedBinder); Mock<IModelBinderProvider> secondProvider = new Mock<IModelBinderProvider>(); Mock<IModelBinderProvider> thirdProvider = new Mock<IModelBinderProvider>(); IModelBinderProvider[] dependencyProviders = new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object }; IModelBinderProvider[] collectionProviders = new IModelBinderProvider[] { thirdProvider.Object }; Mock<IDependencyResolver> resolver = new Mock<IDependencyResolver>(); resolver.Setup(r => r.GetServices(typeof(IModelBinderProvider))).Returns(dependencyProviders); ModelBinderProviderCollection providers = new ModelBinderProviderCollection(collectionProviders, resolver.Object); // Act IModelBinder returnedBinder = providers.GetBinder(modelType); // Assert Assert.Same(expectedBinder, returnedBinder); }