-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
OnFailureTests.cs
177 lines (141 loc) · 5.68 KB
/
OnFailureTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
namespace FluentValidation.Tests {
using System.Threading.Tasks;
using System.Diagnostics;
using Xunit;
using static ComplexValidationTester;
using TestHelper;
public class OnFailureTests {
private TestValidator _validator;
public OnFailureTests() {
_validator = new TestValidator();
}
[Fact]
public void OnFailure_called_for_each_failed_rule() {
_validator.CascadeMode = CascadeMode.Continue;
int invoked = 0;
_validator.RuleFor(person => person.Surname).NotNull().NotEmpty().OnFailure(person => {
invoked += 1;
});
_validator.RuleFor(person => person.Surname).NotEmpty().OnFailure((person, ctx, value) => {
Debug.WriteLine(ctx.PropertyName);
invoked += 1;
});
_validator.RuleFor(person => person.Forename).NotEqual("John").OnFailure((person, ctx, value) => {
invoked += 1;
});
_validator.RuleFor(person => person.Age).GreaterThanOrEqualTo(18).OnFailure((person, ctx, value) => {
invoked += 1;
});
_validator.Validate(new Person { Forename = "John", Age = 17 });
invoked.ShouldEqual(4);
}
[Fact]
public async Task OnFailure_called_for_each_failed_rule_asyncAsync() {
_validator.CascadeMode = CascadeMode.Continue;
int invoked = 0;
_validator.RuleFor(person => person.Surname).NotNull().NotEmpty().OnFailure(person => {
invoked += 1;
});
_validator.RuleFor(person => person.Surname).NotEmpty().OnFailure((person, ctx, value) => {
Debug.WriteLine(ctx.PropertyName);
invoked += 1;
});
_validator.RuleFor(person => person.Forename).NotEqual("John").OnFailure((person, ctx, value) => {
invoked += 1;
});
_validator.RuleFor(person => person.Age).GreaterThanOrEqualTo(18).OnFailure((person, ctx, value) => {
invoked += 1;
});
await _validator.ValidateAsync(new Person { Forename = "John", Age = 17 });
invoked.ShouldEqual(4);
}
[Fact]
public void Should_be_able_to_access_error_message_in_OnFailure() {
_validator.CascadeMode = CascadeMode.Continue;
string errorMessage = string.Empty;
_validator.RuleFor(person => person.Surname).NotNull().WithMessage("You must specify Surname!").OnFailure((person,ctx, value, message) => {
errorMessage = message;
});
_validator.Validate(new Person());
errorMessage.ShouldEqual("You must specify Surname!");
}
[Fact]
public void ShouldHaveChildValidator_should_be_true() {
_validator.RuleFor(person => person.Address).SetValidator(new AddressValidatorWithOnFailure()).OnFailure((p,ctx, value)=> { Debug.WriteLine(p.Forename); });
_validator.ShouldHaveChildValidator(x => x.Address, typeof(AddressValidatorWithOnFailure));
}
[Fact]
public void ShouldHaveChildValidator_works_with_Include() {
_validator.Include(new InlineValidator<Person>() {
v => v.RuleFor(x => x.Forename).NotNull(),
});
_validator.ShouldHaveChildValidator(x => x, typeof(InlineValidator<Person>));
}
[Fact]
public void WhenWithOnFailure_should_invoke_condition_on_inner_validator() {
bool shouldNotBeTrue = false;
var validator = new TestValidator();
validator.RuleFor(x => x.Surname)
.NotEqual("foo")
.When(x => x.Id > 0)
.OnFailure(x => shouldNotBeTrue = true);
var result = validator.Validate(new Person {Id = 0, Surname = "foo"});
result.Errors.Count.ShouldEqual(0);
shouldNotBeTrue.ShouldBeFalse();
}
[Fact]
public async Task WhenAsyncWithOnFailure_should_invoke_condition_on_inner_validator() {
bool shouldNotBeTrue = false;
var validator = new TestValidator();
validator.RuleFor(x => x.Surname)
.NotEqual("foo")
.WhenAsync((x, token) => Task.FromResult(x.Id > 0))
.OnFailure(x => shouldNotBeTrue = true);
var result = await validator.ValidateAsync(new Person {Id = 0, Surname = "foo"});
result.Errors.Count.ShouldEqual(0);
shouldNotBeTrue.ShouldBeFalse();
}
[Fact]
public void WhenAsyncWithOnFailure_throws_when_async_condition_on_inner_validator_invoked_synchronously() {
var validator = new TestValidator();
validator.RuleFor(x => x.Surname)
.NotEqual("foo")
.WhenAsync((x, token) => Task.FromResult(x.Id > 0))
.OnFailure(x => {});
Assert.Throws<AsyncValidatorInvokedSynchronouslyException>(() =>
validator.Validate(new Person {Id = 0, Surname = "foo"}));
}
[Fact]
public async Task WhenWithOnFailure_should_invoke_condition_on_async_inner_validator() {
bool shouldNotBeTrue = false;
var validator = new TestValidator();
validator.RuleFor(x => x.Surname)
.MustAsync((x, ctx) => Task.FromResult(false))
.When(x => x.Id > 0)
.OnFailure(x => shouldNotBeTrue = true);
var result = await validator.ValidateAsync(new Person {Id = 0, Surname = "foo"});
result.Errors.Count.ShouldEqual(0);
shouldNotBeTrue.ShouldBeFalse();
}
[Fact]
public async Task WhenAsyncWithOnFailure_should_invoke_condition_on_async_inner_validator() {
bool shouldNotBeTrue = false;
var validator = new TestValidator();
validator.RuleFor(x => x.Surname)
.MustAsync((x, ctx) => Task.FromResult(false))
.WhenAsync((x, ctx) => Task.FromResult(x.Id > 0))
.OnFailure(x => shouldNotBeTrue = true);
var result = await validator.ValidateAsync(new Person {Id = 0, Surname = "foo"});
result.Errors.Count.ShouldEqual(0);
shouldNotBeTrue.ShouldBeFalse();
}
}
public class AddressValidatorWithOnFailure : AbstractValidator<Address> {
public AddressValidatorWithOnFailure() {
RuleFor(x => x.Postcode).NotNull().OnFailure((address, ctx, value) => {
Debug.WriteLine(address.Line1);
});
RuleFor(x => x.Country).SetValidator(new CountryValidator());
}
}
}