public void GetBinderResolvesBindersWithCorrectPrecedence() { // Proper order of precedence: // 1. Binder registered in the global table // 2. Binder attribute defined on the type // 3. Default binder // Arrange IModelBinder registeredFirstBinder = new Mock <IModelBinder>().Object; ModelBinderDictionary binders = new ModelBinderDictionary() { { typeof(MyFirstConvertibleType), registeredFirstBinder } }; IModelBinder defaultBinder = new Mock <IModelBinder>().Object; binders.DefaultBinder = defaultBinder; // Act IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType)); IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType)); IModelBinder binder3 = binders.GetBinder(typeof(object)); // Assert Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table."); Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type."); Assert.AreSame(defaultBinder, binder3, "Third binder should have been the fallback."); }
public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo() { // Proper order of precedence: // 1. Binder registered in the global table // 2. Binder attribute defined on the type // 3. <null> // Arrange IModelBinder registeredFirstBinder = new Mock <IModelBinder>().Object; ModelBinderDictionary binders = new ModelBinderDictionary() { { typeof(MyFirstConvertibleType), registeredFirstBinder } }; // Act IModelBinder binder1 = binders.GetBinder( typeof(MyFirstConvertibleType), false /* fallbackToDefault */ ); IModelBinder binder2 = binders.GetBinder( typeof(MySecondConvertibleType), false /* fallbackToDefault */ ); IModelBinder binder3 = binders.GetBinder( typeof(object), false /* fallbackToDefault */ ); // Assert Assert.Same(registeredFirstBinder, binder1); Assert.IsType <MySecondBinder>(binder2); Assert.Null(binder3); }
public void GetBinderResolvesBindersWithCorrectPrecedence() { // Proper order of precedence: // 1. Binder registered in the global table // 2. Binder attribute defined on the type // 3. Default binder // Arrange IModelBinder registeredFirstBinder = new Mock <IModelBinder>().Object; ModelBinderDictionary binders = new ModelBinderDictionary() { { typeof(MyFirstConvertibleType), registeredFirstBinder } }; IModelBinder defaultBinder = new Mock <IModelBinder>().Object; binders.DefaultBinder = defaultBinder; // Act IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType)); IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType)); IModelBinder binder3 = binders.GetBinder(typeof(object)); // Assert Assert.Same(registeredFirstBinder, binder1); Assert.IsType <MySecondBinder>(binder2); Assert.Same(defaultBinder, binder3); }
public void GetBinderThrowsIfModelTypeIsNull() { // Arrange ModelBinderDictionary binders = new ModelBinderDictionary(); // Act & Assert Assert.ThrowsArgumentNull( delegate { binders.GetBinder(null); }, "modelType"); }
public void ShouldResolveBinder() { var dictionary = new ModelBinderDictionary(new DefaultModelBinder()); dictionary.Add(typeof(DataSet), FakeBinder); var binder = dictionary.GetBinder(typeof(DataSet)); Assert.AreSame(FakeBinder, binder); }
public void GetBinderThrowsIfModelTypeContainsMultipleAttributes() { // Arrange ModelBinderDictionary binders = new ModelBinderDictionary(); // Act & Assert Assert.Throws <InvalidOperationException>( delegate { binders.GetBinder(typeof(ConvertibleTypeWithSeveralBinders), true /* fallbackToDefault */); }, "The type 'System.Web.Mvc.Test.ModelBinderDictionaryTest+ConvertibleTypeWithSeveralBinders' contains multiple attributes that inherit from CustomModelBinderAttribute."); }
public void ShouldResolveBindersViaInheritance() { var dictionary = new ModelBinderDictionary(new DefaultModelBinder()); dictionary.Add(typeof(IListSource), FakeBinder); var binder = dictionary.GetBinder(typeof(DataSet)); // DataSet implements IListSource Assert.AreSame(FakeBinder, binder); }
public void ShouldFallBackToDefaultBinder() { var dictionary = new ModelBinderDictionary(new DefaultModelBinder()); dictionary.Add(typeof(DataSet), FakeBinder); var binder = dictionary.GetBinder(typeof(int)); Assert.IsInstanceOf <DefaultModelBinder>(binder); }
public void ShouldRespectExplicitDefaultBinder() { var dictionary = new ModelBinderDictionary(new DefaultModelBinder()); dictionary.DefaultModelBinder = FakeBinder; var binder = dictionary.GetBinder(typeof(int)); Assert.AreSame(FakeBinder, binder); }
public void GetBinderThrowsIfModelTypeIsNull() { // Arrange ModelBinderDictionary binders = new ModelBinderDictionary(); // Act & Assert ExceptionHelper.ExpectArgumentNullException( delegate { binders.GetBinder(null); }, "modelType"); }
public T GetModel <T>() { var binder = _binders.GetBinder(typeof(T)); var res = binder.BindModel(_context, new ModelBindingContext { FallbackToEmptyPrefix = true, ModelMetadata = new ModelMetadata(new EmptyModelMetadataProvider(), null, () => default(T), typeof(T), ""), ValueProvider = _values }); return((res is T) ? (T)res : default(T)); }
public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo() { // Proper order of precedence: // 1. Binder registered in the global table // 2. Binder attribute defined on the type // 3. <null> // Arrange IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object; ModelBinderDictionary binders = new ModelBinderDictionary() { { typeof(MyFirstConvertibleType), registeredFirstBinder } }; // Act IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */); IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */); IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */); // Assert Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table."); Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type."); Assert.IsNull(binder3, "Third binder should have returned null since asked not to use default."); }
public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo() { // Proper order of precedence: // 1. Binder registered in the global table // 2. Binder attribute defined on the type // 3. <null> // Arrange IModelBinder registeredFirstBinder = new Mock <IModelBinder>().Object; ModelBinderDictionary binders = new ModelBinderDictionary() { { typeof(MyFirstConvertibleType), registeredFirstBinder } }; // Act IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */); IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */); IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */); // Assert Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table."); Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type."); Assert.IsNull(binder3, "Third binder should have returned null since asked not to use default."); }
public void GetBinderConsultsProviders() { //Arrange Type modelType = typeof(string); IModelBinder expectedBinderFromProvider = new Mock<IModelBinder>().Object; Mock<IModelBinderProvider> locatedProvider = new Mock<IModelBinderProvider>(); locatedProvider.Setup(p => p.GetBinder(modelType)) .Returns(expectedBinderFromProvider); Mock<IModelBinderProvider> secondProvider = new Mock<IModelBinderProvider>(); ModelBinderProviderCollection providers = new ModelBinderProviderCollection(new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object }); ModelBinderDictionary binders = new ModelBinderDictionary(providers); //Act IModelBinder returnedBinder = binders.GetBinder(modelType); //Assert Assert.AreSame(expectedBinderFromProvider, returnedBinder); }
public void GetBinderConsultsProviders() { //Arrange Type modelType = typeof(string); IModelBinder expectedBinderFromProvider = new Mock <IModelBinder>().Object; Mock <IModelBinderProvider> locatedProvider = new Mock <IModelBinderProvider>(); locatedProvider.Setup(p => p.GetBinder(modelType)) .Returns(expectedBinderFromProvider); Mock <IModelBinderProvider> secondProvider = new Mock <IModelBinderProvider>(); ModelBinderProviderCollection providers = new ModelBinderProviderCollection(new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object }); ModelBinderDictionary binders = new ModelBinderDictionary(providers); //Act IModelBinder returnedBinder = binders.GetBinder(modelType); //Assert Assert.AreSame(expectedBinderFromProvider, returnedBinder); }
public void RegisterBinderPopulatesModelBinderDictionary() { // Arrange ModelBinderDictionary binderDict = new ModelBinderDictionary(); // Act FileCollectionModelBinder.RegisterBinder(binderDict); // Assert Assert.AreEqual(6, binderDict.Count); Assert.IsInstanceOfType(binderDict.GetBinder(typeof(HttpPostedFileBase[])), typeof(FileCollectionModelBinder)); Assert.IsInstanceOfType(binderDict.GetBinder(typeof(IEnumerable <HttpPostedFileBase>)), typeof(FileCollectionModelBinder)); Assert.IsInstanceOfType(binderDict.GetBinder(typeof(ICollection <HttpPostedFileBase>)), typeof(FileCollectionModelBinder)); Assert.IsInstanceOfType(binderDict.GetBinder(typeof(IList <HttpPostedFileBase>)), typeof(FileCollectionModelBinder)); Assert.IsInstanceOfType(binderDict.GetBinder(typeof(Collection <HttpPostedFileBase>)), typeof(FileCollectionModelBinder)); Assert.IsInstanceOfType(binderDict.GetBinder(typeof(List <HttpPostedFileBase>)), typeof(FileCollectionModelBinder)); }
public void GetBinderThrowsIfModelTypeContainsMultipleAttributes() { // Arrange ModelBinderDictionary binders = new ModelBinderDictionary(); // Act & Assert ExceptionHelper.ExpectInvalidOperationException( delegate { binders.GetBinder(typeof(ConvertibleTypeWithSeveralBinders), true /* fallbackToDefault */); }, "The type 'System.Web.Mvc.Test.ModelBinderDictionaryTest+ConvertibleTypeWithSeveralBinders' contains multiple attributes that inherit from CustomModelBinderAttribute."); }
public DefaultEncoding() { this.binder = binderDictionary.GetBinder(typeof(T), fallbackToDefault: true); }
public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo() { // Proper order of precedence: // 1. Binder registered in the global table // 2. Binder attribute defined on the type // 3. <null> // Arrange IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object; ModelBinderDictionary binders = new ModelBinderDictionary() { { typeof(MyFirstConvertibleType), registeredFirstBinder } }; // Act IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */); IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */); IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */); // Assert Assert.Same(registeredFirstBinder, binder1); Assert.IsType<MySecondBinder>(binder2); Assert.Null(binder3); }
private MvcEncoderDecoder() { this.binder = binderDictionary.GetBinder(typeof(T), fallbackToDefault: true); }
public void GetBinderResolvesBindersWithCorrectPrecedence() { // Proper order of precedence: // 1. Binder registered in the global table // 2. Binder attribute defined on the type // 3. Default binder // Arrange IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object; ModelBinderDictionary binders = new ModelBinderDictionary() { { typeof(MyFirstConvertibleType), registeredFirstBinder } }; IModelBinder defaultBinder = new Mock<IModelBinder>().Object; binders.DefaultBinder = defaultBinder; // Act IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType)); IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType)); IModelBinder binder3 = binders.GetBinder(typeof(object)); // Assert Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table."); Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type."); Assert.AreSame(defaultBinder, binder3, "Third binder should have been the fallback."); }
public void GetBinderResolvesBindersWithCorrectPrecedence() { // Proper order of precedence: // 1. Binder registered in the global table // 2. Binder attribute defined on the type // 3. Default binder // Arrange IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object; ModelBinderDictionary binders = new ModelBinderDictionary() { { typeof(MyFirstConvertibleType), registeredFirstBinder } }; IModelBinder defaultBinder = new Mock<IModelBinder>().Object; binders.DefaultBinder = defaultBinder; // Act IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType)); IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType)); IModelBinder binder3 = binders.GetBinder(typeof(object)); // Assert Assert.Same(registeredFirstBinder, binder1); Assert.IsType<MySecondBinder>(binder2); Assert.Same(defaultBinder, binder3); }