예제 #1
0
 public void ValidateBindingContextThrowsIfBindingContextIsNull()
 {
     // Act & assert
     Assert.ThrowsArgumentNull(
         () => ModelBindingHelper.ValidateBindingContext(null, typeof(string), true),
         "bindingContext");
 }
예제 #2
0
        public void ValidateBindingContextThrowsIfModelMetadataIsNull()
        {
            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext();

            // Act & assert
            Assert.Throws <ArgumentException>(
                () => ModelBindingHelper.ValidateBindingContext(bindingContext, typeof(string), true),
                @"The binding context cannot have a null ModelMetadata.
Parameter name: bindingContext");
        }
예제 #3
0
        public void ValidateBindingContextThrowsIfModelMetadataIsNull()
        {
            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext();

            // Act & assert
            Assert.ThrowsArgument(
                () => ModelBindingHelper.ValidateBindingContext(bindingContext, typeof(string), true),
                "bindingContext",
                "The binding context cannot have a null ModelMetadata.");
        }
예제 #4
0
        public void ValidateBindingContext_SuccessWithNullModel()
        {
            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = GetMetadata(typeof(string))
            };

            // Act
            ModelBindingHelper.ValidateBindingContext(bindingContext, typeof(string), true);
            // Assert
            // Nothing to do - if we got this far without throwing, the test succeeded
        }
예제 #5
0
        public void ValidateBindingContextThrowsIfModelTypeIsWrong()
        {
            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = GetMetadata(typeof(object))
            };

            // Act & assert
            Assert.Throws <ArgumentException>(
                () => ModelBindingHelper.ValidateBindingContext(bindingContext, typeof(string), true),
                @"The binding context has a ModelType of 'System.Object', but this binder can only operate on models of type 'System.String'.
Parameter name: bindingContext");
        }
예제 #6
0
        public void ValidateBindingContextThrowsIfModelIsNullButCannotBe()
        {
            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = GetMetadata(typeof(string))
            };

            // Act & assert
            Assert.Throws <ArgumentException>(
                () => ModelBindingHelper.ValidateBindingContext(bindingContext, typeof(string), false),
                @"The binding context has a null Model, but this binder requires a non-null model of type 'System.String'.
Parameter name: bindingContext");
        }
예제 #7
0
        public void ValidateBindingContextThrowsIfModelInstanceIsWrongType()
        {
            // Arrange
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = GetMetadata(typeof(string))
            };

            bindingContext.ModelMetadata.Model = 42;

            // Act & assert
            Assert.ThrowsArgument(
                () => ModelBindingHelper.ValidateBindingContext(bindingContext, typeof(string), allowNullModel: true),
                "bindingContext",
                "The binding context has a Model of type 'System.Int32', but this binder can only operate on models of type 'System.String'.");
        }