public void NotDuplicateExpenseDetailsValidation()
        {
            var aExpenseValidator = this.valFactoryFromConfig.CreateValidator <Model.Expense>("ExpenseRuleset");

            var expenseToValidate = StubsHelper.GenerateExpenseStub();

            expenseToValidate.Details = new List <Model.ExpenseItem>
            {
                new Model.ExpenseItem()
                {
                    Description = "Test"
                },
                new Model.ExpenseItem()
                {
                    Description = "Test"
                }
            };

            ValidationResults results = aExpenseValidator.Validate(expenseToValidate);

            Assert.IsFalse(results.IsValid);
            Assert.AreEqual(1, results.Count);

            var result = results.FirstOrDefault();

            Assert.IsNotNull(result);

            StringAssert.Equals(Resources.DuplicateExpenseDetailsValidation, result.Message);
        }
Beispiel #2
0
        public static void Exists(ValidationResults results, string key, string error)
        {
            var result = results.FirstOrDefault(r => r.Key.Equals(key) &&
                                                r.Message.Equals(error)) != null;

            if (!result)
            {
                Assert.True(false, "Validation Rule has no match. Key = '{0}', Expected = '{1}'.\r\nRules Failed:\r\n{2}"
                            .FormatWith(key, error, Dump(results)));
            }
        }
Beispiel #3
0
        public string GetFirstErrorMessage()
        {
            var errorMessage = string.Empty;

            if (ValidationResults.Any())
            {
                var commandValidationResult = ValidationResults.FirstOrDefault();
                if (commandValidationResult != null)
                {
                    errorMessage = commandValidationResult.ErrorMessage;
                }
            }

            return(errorMessage);
        }
 public string this[string propertyName] {
     get {
         if (results != null && !results.IsValid)
         {
             ValidationResult result = results.FirstOrDefault(
                 r => String.CompareOrdinal(r.Key, propertyName) == 0
                 );
             if (result != null)
             {
                 return(result.Message);
             }
         }
         return(string.Empty);
     }
 }
        /// <summary>
        /// Called by
        /// <see cref="ValidationResultTester{TModel}.WithMessage(string)"/>,
        /// <see cref="ValidationResultTester{TModel}.WithErrorMessageContaining(string)"/>,
        /// <see cref="ValidationResultTester{TModel}.WithErrorMessageMatching(string)"/>,
        /// <see cref="ValidationResultTester{TModel}.WithErrorMessageStartingWith(string)"/>.
        /// </summary>
        /// <param name="memberNames">The member names expected in the <see cref="ValidationResult"/>.</param>
        /// <param name="predicate">Predication for successful error message match.</param>
        /// <param name="errorMessage">Message to be included in the exception if no match is found.</param>
        /// <returns></returns>
        internal ValidationForModel <TModel> AssertResultMatch(
            IEnumerable <string> memberNames, Func <string, bool> predicate, string errorMessage)
        {
            string formattedMemberNames = FormatMemberNames(memberNames);

            ValidationResult result = ValidationResults.FirstOrDefault(x =>
                                                                       FormatMemberNames(x.MemberNames) == formattedMemberNames && predicate(x.ErrorMessage));

            if (result == null)
            {
                throw new ValidationTestException(
                          $"Expected {errorMessage}. {formattedMemberNames}. Found:\n{FormattedValidationResults}\n");
            }

            return(this);
        }
Beispiel #6
0
        public virtual string this[string columnName]
        {
            get
            {
                string message         = null;
                var    filteredResults = new ValidationResults();

                var result = _validationResults.FirstOrDefault(x => x.Key == columnName);
                if (result != null)
                {
                    filteredResults.AddResult(result);
                    message = filteredResults.First().Message;
                }
                return(message);
            }
        }
        public void UserShouldNotBeNullValidation()
        {
            var aExpenseValidator = this.valFactory.CreateValidator <Model.Expense>();

            var expenseToValidate = StubsHelper.GenerateExpenseStub();

            expenseToValidate.User = null;

            ValidationResults results = aExpenseValidator.Validate(expenseToValidate);

            Assert.IsFalse(results.IsValid);
            Assert.AreEqual(1, results.Count);

            var result = results.FirstOrDefault();

            Assert.IsNotNull(result);

            StringAssert.Equals(Resources.UserNotNullMessage, result.Message);
        }
        public void TitleMustBeSuppliedValidation()
        {
            var aExpenseValidator = this.valFactory.CreateValidator <Model.Expense>();

            var expenseToValidate = StubsHelper.GenerateExpenseStub();

            expenseToValidate.Title = string.Empty;

            ValidationResults results = aExpenseValidator.Validate(expenseToValidate);

            Assert.IsFalse(results.IsValid);
            Assert.AreEqual(1, results.Count);

            var result = results.FirstOrDefault();

            Assert.IsNotNull(result);

            StringAssert.Equals(Resources.TitleMustBeSuppliedMessage, result.Message);
        }
        public void AmountMustBePositiveValidation()
        {
            var aExpenseItemValidator = this.valFactory.CreateValidator <Model.ExpenseItem>();

            var expenseItemToValidate = StubsHelper.GenerateExpenseItemStub();

            expenseItemToValidate.Amount = -1;

            ValidationResults results = aExpenseItemValidator.Validate(expenseItemToValidate);

            Assert.IsFalse(results.IsValid);
            Assert.AreEqual(1, results.Count);

            var result = results.FirstOrDefault();

            Assert.IsNotNull(result);

            StringAssert.Equals(Resources.AmountGreaterThanZeroMessage, result.Message);
        }