Esempio n. 1
0
        public void CanValidateModelTest()
        {
            // Arrange
            var grantAllocation           = TestFramework.TestGrantAllocation.Create();
            var viewModel                 = new EditGrantAllocationViewModel(grantAllocation);
            var nameOfGrantAllocationName = GeneralUtility.NameOf(() => viewModel.GrantAllocationName);

            ICollection <ValidationResult> validationResults;

            // Act
            DataAnnotationsValidator.TryValidate(viewModel, out validationResults);

            // Assert
            Assert.That(validationResults.Count, Is.EqualTo(1), "Expecting certain number of errors");
            TestFramework.AssertFieldRequired(validationResults, nameOfGrantAllocationName);

            // Act
            // Set string fields to string longer than their max lengths
            viewModel.GrantAllocationName = TestFramework.MakeTestNameLongerThan(nameOfGrantAllocationName, Models.GrantAllocation.FieldLengths.GrantAllocationName);
            DataAnnotationsValidator.TryValidate(viewModel, out validationResults);

            // Assert
            Assert.That(validationResults.Count, Is.EqualTo(1), "Expecting certain number of errors");
            TestFramework.AssertFieldStringLength(validationResults, nameOfGrantAllocationName, Models.GrantAllocation.FieldLengths.GrantAllocationName);

            // Act
            // Happy path
            viewModel.GrantAllocationName = TestFramework.MakeTestName(nameOfGrantAllocationName, Models.GrantAllocation.FieldLengths.GrantAllocationName);
            var isValid = DataAnnotationsValidator.TryValidate(viewModel, out validationResults);

            // Assert
            Assert.That(isValid, Is.True, "Should pass validation");
        }
        public void CanValidateModelTest()
        {
            // Arrange
            var tag = TestFramework.TestTag.Create();

            // Inducing an error in Tag
            tag.TagName = null;
            var viewModel     = new EditViewModel(tag);
            var nameOfTagName = GeneralUtility.NameOf(() => viewModel.TagName);

            ICollection <ValidationResult> validationResults;

            // Act
            DataAnnotationsValidator.TryValidate(viewModel, out validationResults);

            // Assert
            Assert.That(validationResults.Count, Is.EqualTo(1), "Expecting certain number of errors");
            TestFramework.AssertFieldRequired(validationResults, nameOfTagName);

            // Act
            // Set string fields to string longer than their max lengths
            viewModel.TagName = TestFramework.MakeTestNameLongerThan(nameOfTagName, Models.Tag.FieldLengths.TagName);
            DataAnnotationsValidator.TryValidate(viewModel, out validationResults);

            // Assert
            Assert.That(validationResults.Count, Is.AtLeast(1), "Expecting certain number of errors");
            TestFramework.AssertFieldStringLength(validationResults, nameOfTagName, Models.Tag.FieldLengths.TagName);

            // Act
            // Set string fields to string longer than their max lengths
            viewModel.TagName = TestFramework.MakeTestName(string.Format("{0}&", nameOfTagName), Models.Tag.FieldLengths.TagName);
            DataAnnotationsValidator.TryValidate(viewModel, out validationResults);


            // Assert
            Assert.That(validationResults.Count, Is.AtLeast(1), "Expecting certain number of errors");
            TestFramework.AssertInvalidCharacters(validationResults);

            // Act
            // Happy path
            viewModel.TagName = TestFramework.MakeTestNameWithoutCertainCharacters(nameOfTagName, Models.Tag.FieldLengths.TagName, @"[^a-zA-Z0-9-_\s]");
            var isValid = DataAnnotationsValidator.TryValidate(viewModel, out validationResults);

            // Assert
            Assert.That(isValid, Is.True, "Should pass validation");
        }