Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Validation is not triggered when a property is compared with another property - UNIT TEST ATTACHED -

$
0
0

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; }

 


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>