Hello Jeremy,
this unit test should actually show an error but it does not.
I guess the logic of my RuleFor is not correct.
This is what I want to achieve:
When IsFolder property is false (by default) and the SelectedFolderId equals 2 (teststep) then the children unit can not be created.
What do I wrong?
[Test] publicvoid CreateUnit_FolderTypeIsTeststepAndIsFolderIsFalse_ReturnsIsNotValid() { new UnitViewModelValidator().ShouldHaveValidationErrorFor(x => x.SelectedFolderTypeId, new UnitViewModel() { SelectedFolderTypeId = 2, IsFolder = false, }); }
publicclass UnitViewModelValidator : AbstractValidator<UnitViewModel> { public UnitViewModelValidator() { RuleFor(r => r.Name) .NotEmpty().WithMessage("Name must not be empty") .Length(1, 30).WithMessage("Name must not be longer than 30 chars."); RuleFor(r => r.SelectedFolderTypeId) .Equal(2) .When(u => u.IsFolder == false).WithMessage("A child unit of type teststep can only be created for a unit folder."); } }
[FluentValidation.Attributes.Validator(typeof(UnitViewModel))] publicclass UnitViewModel { public UnitViewModel() { DisplayList = new List<UnitFolderTypeViewModel>() { new UnitFolderTypeViewModel{ Id = 1, Name = "folder.png"}, new UnitFolderTypeViewModel{ Id = 2, Name = "teststep.png"}, }; } [Remote("UnitExists", "Unit", ErrorMessage = "This name already exists.", AdditionalFields = "TemplateId")] publicstring Name { get; set; } public Nullable<int> ParentId { get; set; } publicint TemplateId { get; set; } publicint UnitId { get; set; } publicbool IsFolder { get; set; } publicint SelectedFolderTypeId { get; set; } public IEnumerable<UnitFolderTypeViewModel> DisplayList { get; set; }