public CountriesControllerTests() { validator = Substitute.For <ICountryValidator>(); service = Substitute.For <ICountryService>(); country = ObjectFactory.CreateCountryView(); controller = Substitute.ForPartsOf <CountriesController>(validator, service); controller.ControllerContext = new ControllerContext { RouteData = new RouteData() }; }
/// <summary> /// Initializes a new instance of the <see cref="AssetValidator"/> class. /// </summary> public AssetValidator(ICountryValidator countryValidator) { RuleFor(x => x.AssetName).NotNull().WithMessage(Constants.ASSET_NAME_REQUIRED).MinimumLength(ASSET_NAME_LENGHT).WithMessage(string.Format(Constants.ASSET_NAME_LENGTH, ASSET_NAME_LENGHT)); RuleFor(x => x.Department).NotNull().WithMessage(Constants.DEPARTMENT_REQUIRED).IsInEnum().WithMessage(Constants.DEPARTMENT_INVALID); RuleFor(x => x.PurchaseDate).NotNull().WithMessage(Constants.PURCHASE_DATE_REQUIRED).Must(curDate => curDate >= DateTime.Now.AddDays(-365)).WithMessage(Constants.PURCHASE_DATE_INVALID); RuleFor(x => x.EMailAddressOfDepartment).NotNull().WithMessage(Constants.EMAIL_REQUIRED).EmailAddress().WithMessage(Constants.EMAIL_INVALID); RuleFor(x => x.CountryOfDepartment).NotNull().WithMessage(Constants.COUNTRY_REQUIRED).Must(a => { if (!string.IsNullOrEmpty(a)) { return(countryValidator.ValidateCountryByName(a)); } else { return(true); } }).WithMessage(Constants.COUNTRY_INVALID); }
//Validate CountryName protected virtual ValidationResult ValidateCountryName(Int16 idCountry, string countryName) { ValidationResult validationResult = new ValidationResult(true); try { //Create Validator based on its Key name : 'Keywords.UpdateCountry'. //The validator factory will create the validator object based on its key. ICountryValidator validator = (ICountryValidator)ValidatorFactory.Create(Keywords.UpdateCountry); //Execute validation process validationResult = validator.ValidateCountryName(idCountry, countryName); } catch { //The program will throw error if the validation object doesn't exist. And the default of validation result is true. } return(validationResult); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value == null) { return(ValidationResult.Success); } ICountryValidator validator = ServiceLocator.Current.GetService <ICountryValidator>(); var result = new ValidationResult(this.ErrorMessage); if (validator == null) { return(result); } bool validationResult = validator.IsCountryValid(value.ToString()); return(validationResult ? ValidationResult.Success : result); }
public CountryService(IUnitOfWork UnitOfWork, ICountryValidator CountryValidator) : base(UnitOfWork) { this.CountryValidator = CountryValidator; }