@@ -42,7 +42,7 @@ public async Task Handle_WhenNoValidatorsProvided_ShouldProceedToNextHandler()
4242 ) ;
4343
4444 // Assert
45- result . Should ( ) . Be ( result ) ;
45+ result . Should ( ) . Be ( _response ) ;
4646 }
4747
4848 [ Trait ( "Category" , "Unit" ) ]
@@ -65,7 +65,7 @@ public async Task Handle_WhenValidationPasses_ShouldProceedToNextHandler()
6565 ) ;
6666
6767 // Assert
68- result . Should ( ) . Be ( result ) ;
68+ result . Should ( ) . Be ( _response ) ;
6969 _mockValidator . Verify ( x => x . Validate (
7070 It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
7171 }
@@ -96,16 +96,16 @@ public async Task Handle_WhenSingleValidatorFails_ShouldThrowValidationException
9696 ) ;
9797
9898 // Assert
99- await exception . Should ( ) . ThrowExactlyAsync < ApplicationLayer . ValidationException > ( ) ;
100-
10199 var thrownException = await exception . Should ( ) . ThrowExactlyAsync < ApplicationLayer . ValidationException > ( ) ;
102- thrownException . Which . Messages . Should ( ) . HaveCount ( 2 ) ;
103- thrownException . Which . Messages . Should ( )
104- . Contain ( e => e . PropertyName == "Name" && e . ErrorMessage == "Name is required" ) ;
105- thrownException . Which . Messages . Should ( )
106- . Contain ( e => e . PropertyName == "Age" && e . ErrorMessage == "Age must be between 20 and 50" ) ;
107-
108- _mockValidator . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Exactly ( 2 ) ) ;
100+
101+ thrownException . Which . Details . Should ( ) . NotBeNull ( ) ;
102+ thrownException . Which . Details . Should ( ) . HaveCount ( 2 ) ;
103+ thrownException . Which . Details . Should ( ) . ContainKey ( "Name" ) ;
104+ thrownException . Which . Details . Should ( ) . ContainKey ( "Age" ) ;
105+ thrownException . Which . Details [ "Name" ] . Should ( ) . Equal ( "Name is required" ) ;
106+ thrownException . Which . Details [ "Age" ] . Should ( ) . Equal ( "Age must be between 20 and 50" ) ;
107+
108+ _mockValidator . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
109109 }
110110
111111 [ Trait ( "Category" , "Unit" ) ]
@@ -138,19 +138,23 @@ public async Task Handle_WhenMultipleValidatorsFail_ShouldThrowValidationExcepti
138138 ) ;
139139
140140 // Assert
141- await exception . Should ( ) . ThrowExactlyAsync < ApplicationLayer . ValidationException > ( ) ;
142-
143141 var thrownException = await exception . Should ( ) . ThrowExactlyAsync < ApplicationLayer . ValidationException > ( ) ;
144- thrownException . Which . Messages . Should ( ) . HaveCount ( 3 ) ;
145- thrownException . Which . Messages . Should ( )
146- . Contain ( e => e . PropertyName == "Name" && e . ErrorMessage == "Name is required" ) ;
147- thrownException . Which . Messages . Should ( )
148- . Contain ( e => e . PropertyName == "Age" && e . ErrorMessage == "Age must be positive" ) ;
149- thrownException . Which . Messages . Should ( )
150- . Contain ( e => e . PropertyName == "Name" && e . ErrorMessage == "Name must be unique" ) ;
151-
152- mockValidator1 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Exactly ( 2 ) ) ;
153- mockValidator2 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Exactly ( 2 ) ) ;
142+
143+ thrownException . Which . Details . Should ( ) . NotBeNull ( ) ;
144+ thrownException . Which . Details . Should ( ) . HaveCount ( 2 ) ;
145+ thrownException . Which . Details . Should ( ) . ContainKey ( "Name" ) ;
146+ thrownException . Which . Details . Should ( ) . ContainKey ( "Age" ) ;
147+
148+ // Name tem 2 erros agrupados
149+ thrownException . Which . Details [ "Name" ] . Should ( ) . HaveCount ( 2 ) ;
150+ thrownException . Which . Details [ "Name" ] . Should ( ) . Contain ( "Name is required" ) ;
151+ thrownException . Which . Details [ "Name" ] . Should ( ) . Contain ( "Name must be unique" ) ;
152+
153+ // Age tem 1 erro
154+ thrownException . Which . Details [ "Age" ] . Should ( ) . Equal ( "Age must be positive" ) ;
155+
156+ mockValidator1 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
157+ mockValidator2 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
154158 }
155159
156160 [ Trait ( "Category" , "Unit" ) ]
@@ -162,7 +166,6 @@ public async Task Handle_WhenCollectingErrorsFromAllValidators_ShouldThrowSingle
162166 var mockValidator2 = new Mock < IValidator < RequestTest > > ( ) ;
163167 var mockValidator3 = new Mock < IValidator < RequestTest > > ( ) ;
164168
165- // First validator fails
166169 mockValidator1 . Setup ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) )
167170 . Returns ( new ValidationResult ( [
168171 new ValidationFailure ( "Name" , "Name is required" )
@@ -187,18 +190,18 @@ public async Task Handle_WhenCollectingErrorsFromAllValidators_ShouldThrowSingle
187190 ) ;
188191
189192 // Assert
190- await exception . Should ( ) . ThrowExactlyAsync < ApplicationLayer . ValidationException > ( ) ;
191-
192193 var thrownException = await exception . Should ( ) . ThrowExactlyAsync < ApplicationLayer . ValidationException > ( ) ;
193- thrownException . Which . Messages . Should ( ) . HaveCount ( 2 ) ;
194- thrownException . Which . Messages . Should ( )
195- . Contain ( e => e . PropertyName == "Name" && e . ErrorMessage == "Name is required" ) ;
196- thrownException . Which . Messages . Should ( )
197- . Contain ( e => e . PropertyName == "Age" && e . ErrorMessage == "Age must be positive" ) ;
198-
199- mockValidator1 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Exactly ( 2 ) ) ;
200- mockValidator2 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Exactly ( 2 ) ) ;
201- mockValidator3 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Exactly ( 2 ) ) ;
194+
195+ thrownException . Which . Details . Should ( ) . NotBeNull ( ) ;
196+ thrownException . Which . Details . Should ( ) . HaveCount ( 2 ) ;
197+ thrownException . Which . Details . Should ( ) . ContainKey ( "Name" ) ;
198+ thrownException . Which . Details . Should ( ) . ContainKey ( "Age" ) ;
199+ thrownException . Which . Details [ "Name" ] . Should ( ) . Equal ( "Name is required" ) ;
200+ thrownException . Which . Details [ "Age" ] . Should ( ) . Equal ( "Age must be positive" ) ;
201+
202+ mockValidator1 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
203+ mockValidator2 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
204+ mockValidator3 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
202205 }
203206
204207 [ Trait ( "Category" , "Unit" ) ]
@@ -230,10 +233,15 @@ public async Task Handle_WhenSomeValidatorsFail_ShouldValidateAllValidators()
230233 ) ;
231234
232235 // Assert
233- await exception . Should ( ) . ThrowExactlyAsync < ApplicationLayer . ValidationException > ( ) ;
234-
236+ var thrownException = await exception . Should ( ) . ThrowExactlyAsync < ApplicationLayer . ValidationException > ( ) ;
237+
235238 // Verifica que TODOS os validators foram chamados
236239 mockValidator1 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
237240 mockValidator2 . Verify ( x => x . Validate ( It . IsAny < ValidationContext < RequestTest > > ( ) ) , Times . Once ) ;
241+
242+ // Verifica que os erros foram agrupados corretamente
243+ thrownException . Which . Details . Should ( ) . HaveCount ( 2 ) ;
244+ thrownException . Which . Details . Should ( ) . ContainKey ( "Name" ) ;
245+ thrownException . Which . Details . Should ( ) . ContainKey ( "Age" ) ;
238246 }
239247}
0 commit comments