Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a ValidationException based on NHibernate.Validator InvalidValue objects.
        /// </summary>
        /// <param name="errors">The errors received from NHibernate.Validator</param>
        /// <returns>A ValidationException or null if there are no errors</returns>
        public static ValidationException ToValidationException(this InvalidValue[] errors)
        {
            if (null != errors && errors.Length > 0)
            {
                ValidationException ex = new ValidationException();

                foreach (InvalidValue val in errors)
                {
                    ex.Errors.Add(val.PropertyName, val.Message);
                }

                return ex;
            }

            return null;
        }
        public void ShouldCopyAllErrors()
        {
            // arrange
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("Email", "Invalid");
            nvc.Add("Email", "Mising");
            nvc.Add("Password", "Mising");

            ValidationException ex = new ValidationException { Errors = nvc };
            ModelStateDictionary modelState = new ModelStateDictionary();

            // act
            ex.ToModelErrors(modelState, null);

            // assert
            Assert.AreEqual(2, modelState["Email"].Errors.Count);
            Assert.AreEqual(1, modelState["Password"].Errors.Count);
        }
        public void ShouldCopyAllErrorsWithPrefix()
        {
            // arrange
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("Email", "Invalid");
            nvc.Add("Email", "Mising");
            nvc.Add("Password", "Mising");

            ValidationException ex = new ValidationException { Errors = nvc };
            ModelStateDictionary modelState = new ModelStateDictionary();

            // act
            ex.ToModelErrors(modelState, "credentials");

            // assert
            Assert.IsFalse(modelState.ContainsKey("Email"));
            Assert.IsFalse(modelState.ContainsKey("Password"));
            Assert.AreEqual(2, modelState["credentials.Email"].Errors.Count);
            Assert.AreEqual(1, modelState["credentials.Password"].Errors.Count);
        }