You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I got a really interesting behavior. I have the two test cases below:
[Fact]
public void Ctor_WhenNeverIsTrueAndAfterOcurrenceIsNotNull_HasError()
{
// arrange
var reccurenceEnd = new RecurrenceEnd()
{
IsNever = true,
AfterOccurence = 1
};
// act
var validator = GetValidator();
// assert
validator.ShouldHaveValidationErrorFor(p => p.AfterOccurence, reccurenceEnd);
}
[Fact]
public void Ctor_WhenNeverIsFalseAndAfterOccurenceIsNullAndByDateIsNull_HasError()
{
// arrange
var reccurenceEnd = new RecurrenceEnd()
{
IsNever = false,
AfterOccurence = null,
ByDate = null
};
// act
var validator = GetValidator();
// assert
validator.ShouldHaveValidationErrorFor(p => p.AfterOccurence, reccurenceEnd);
}
private static RecurrenceEndValidator GetValidator()
{
return new RecurrenceEndValidator();
}
On my validator, if I have the following, the first test fails and the second passes. If I change the order of the rules, the first test passes and the second fails.
using System;
public class RecurrenceEnd
{
public bool IsNever { get; set; }
public int? AfterOccurence { get; set; }
public DateTime? ByDate { get; set; }
}
The text was updated successfully, but these errors were encountered:
Hi, this is related to you use of the When condition. By default, a When condition applies to all previous validators in the same RuleFor call, so when you use a single call to RuleFor, you’re essentially doubling up on conditions.
You should either use separate calls to RuleFor (as you have in your second example), or use the other overload of When that allows you to specify ApplyConditionTo.CurrentValidator
Thanks @JeremySkinner, now it make senses.
Can you give me an example about how I could use the ApplyConditionTo.CurrentValidator in this case, please?
System Details
Issue Description
I got a really interesting behavior. I have the two test cases below:
On my validator, if I have the following, the first test fails and the second passes. If I change the order of the rules, the first test passes and the second fails.
If I change my validator to the following, both tests pass.
My ReccurenceEnd Class is the follow:
The text was updated successfully, but these errors were encountered: