示例#1
0
        public void ModelState_IsValid_ForValidData_OnDerivedModel()
        {
            // Arrange
            var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
            var modelState       = operationContext.ActionContext.ModelState;
            var model            = new SoftwareViewModel
            {
                Category      = "Technology",
                CompanyName   = "Microsoft",
                Contact       = "4258393231",
                Country       = "USA",
                DatePurchased = new DateTime(2010, 10, 10),
                Name          = "MVC",
                Price         = 110,
                Version       = "2"
            };
            var oldModel = model;

            // Act
            var result = TryValidateModel(model, prefix: string.Empty, operationContext: operationContext);

            // Assert
            Assert.True(result);
            Assert.Same(oldModel, model);
            Assert.True(modelState.IsValid);
            var modelStateErrors = GetModelStateErrors(modelState);

            Assert.Empty(modelStateErrors);
        }
        public void ModelState_IsValid_ForValidData_OnDerivedModel()
        {
            // Arrange
            var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
            var modelState = operationContext.ActionContext.ModelState;
            var model = new SoftwareViewModel
            {
                Category = "Technology",
                CompanyName = "Microsoft",
                Contact = "4258393231",
                Country = "USA",
                DatePurchased = new DateTime(2010, 10, 10),
                Name = "MVC",
                Price = 110,
                Version = "2"
            };
            var oldModel = model;

            // Act
            var result = TryValidateModel(model, prefix: string.Empty, operationContext: operationContext);

            // Assert
            Assert.True(result);
            Assert.Same(oldModel, model);
            Assert.True(modelState.IsValid);
            var modelStateErrors = GetModelStateErrors(modelState);
            Assert.Empty(modelStateErrors);
        }
示例#3
0
        public void ModelState_IsInvalid_ForInvalidData_OnDerivedModel()
        {
            // Arrange
            var operationContext = ModelBindingTestHelper.GetOperationBindingContext();

            var modelState = operationContext.ActionContext.ModelState;
            var model      = new SoftwareViewModel
            {
                Category      = "Technology",
                CompanyName   = "Microsoft",
                Contact       = "4258393231",
                Country       = "UK", // Here the validate country is USA only
                DatePurchased = new DateTime(2010, 10, 10),
                Price         = 110,
                Version       = "2"
            };
            var oldModel = model;

            // Act
            var result = TryValidateModel(model, "software", operationContext);

            // Assert
            Assert.False(result);
            Assert.Same(oldModel, model);
            Assert.False(modelState.IsValid);
            var modelStateErrors = GetModelStateErrors(modelState);

            Assert.Single(modelStateErrors);
            Assert.Equal("Product must be made in the USA if it is not named.", modelStateErrors["software"]);
        }
        public void ModelState_IsInvalid_ForInvalidData_OnDerivedModel()
        {
            // Arrange
            var operationContext = ModelBindingTestHelper.GetOperationBindingContext();

            var modelState = operationContext.ActionContext.ModelState;
            var model = new SoftwareViewModel
            {
                Category = "Technology",
                CompanyName = "Microsoft",
                Contact = "4258393231",
                Country = "UK", // Here the validate country is USA only
                DatePurchased = new DateTime(2010, 10, 10),
                Price = 110,
                Version = "2"
            };
            var oldModel = model;

            // Act
            var result = TryValidateModel(model, "software", operationContext);

            // Assert
            Assert.False(result);
            Assert.Same(oldModel, model);
            Assert.False(modelState.IsValid);
            var modelStateErrors = GetModelStateErrors(modelState);
            Assert.Single(modelStateErrors);
            Assert.Equal("Product must be made in the USA if it is not named.", modelStateErrors["software"]);
        }