23 July 2013

Testing DataAnnotations (ViewModel)

using System.ComponentModel.DataAnnotations;     

        [Test]
        public void DogName_Is_Required()
        {
            var dog = new DogViewModel
            {
                Name="Gerka"  
            };
            Assert.IsTrue(ValidateDog(dog).Count > 0);   //We use 0 bec. Count has no setter.
        }

        private List<ValidationResult> ValidateDog (object model)
        {
            var validDog = new List<ValidationResult>();
            var reliable = new ValidationContext(model, null, null);
            Validator.TryValidateObject(model, reliable, validDog, true);
            return validDog;
        }


Note: "Gerka" was the name of our first dog.

No comments:

Post a Comment