1
+ using FluentValidation ;
2
+ using FluentValidation . Results ;
3
+
4
+ using MediatR ;
5
+
6
+ using VerticalSliceArchitecture . Application . Common . Behaviours ;
7
+ using VerticalSliceArchitecture . Application . Domain . Todos ;
8
+ using VerticalSliceArchitecture . Application . Features . TodoLists ;
9
+
10
+ namespace VerticalSliceArchitecture . Application . UnitTests . Common . Behaviours ;
11
+
12
+ public class ValidationBehaviorTests
13
+ {
14
+ private readonly ValidationBehavior < CreateTodoListCommand , ErrorOr < int > > _validationBehavior ;
15
+ private readonly IValidator < CreateTodoListCommand > _mockValidator ;
16
+ private readonly RequestHandlerDelegate < ErrorOr < int > > _mockNextBehavior ;
17
+
18
+ public ValidationBehaviorTests ( )
19
+ {
20
+ _mockNextBehavior = Substitute . For < RequestHandlerDelegate < ErrorOr < int > > > ( ) ;
21
+ _mockValidator = Substitute . For < IValidator < CreateTodoListCommand > > ( ) ;
22
+
23
+ _validationBehavior = new ( _mockValidator ) ;
24
+ }
25
+
26
+ [ Fact ]
27
+ public async Task InvokeValidationBehavior_WhenValidatorResultIsValid_ShouldInvokeNextBehavior ( )
28
+ {
29
+ // Arrange
30
+ var createTodoListCommand = new CreateTodoListCommand ( "Title" ) ;
31
+ var todoList = new TodoList { Title = createTodoListCommand . Title } ;
32
+
33
+ _mockValidator
34
+ . ValidateAsync ( createTodoListCommand , Arg . Any < CancellationToken > ( ) )
35
+ . Returns ( new ValidationResult ( ) ) ;
36
+
37
+ _mockNextBehavior . Invoke ( ) . Returns ( todoList . Id ) ;
38
+
39
+ // Act
40
+ var result = await _validationBehavior . Handle ( createTodoListCommand , _mockNextBehavior , default ) ;
41
+
42
+ // Assert
43
+ result . IsError . Should ( ) . BeFalse ( ) ;
44
+ result . Value . Should ( ) . Be ( todoList . Id ) ;
45
+ }
46
+
47
+ [ Fact ]
48
+ public async Task InvokeValidationBehavior_WhenValidatorResultIsNotValid_ShouldReturnListOfErrors ( )
49
+ {
50
+ // Arrange
51
+ var createTodoListCommand = new CreateTodoListCommand ( "Title" ) ;
52
+ List < ValidationFailure > validationFailures = [ new ( propertyName : "foo" , errorMessage : "bad foo" ) ] ;
53
+
54
+ _mockValidator
55
+ . ValidateAsync ( createTodoListCommand , Arg . Any < CancellationToken > ( ) )
56
+ . Returns ( new ValidationResult ( validationFailures ) ) ;
57
+
58
+ // Act
59
+ var result = await _validationBehavior . Handle ( createTodoListCommand , _mockNextBehavior , default ) ;
60
+
61
+ // Assert
62
+ result . IsError . Should ( ) . BeTrue ( ) ;
63
+ result . FirstError . Code . Should ( ) . Be ( "foo" ) ;
64
+ result . FirstError . Description . Should ( ) . Be ( "bad foo" ) ;
65
+ }
66
+
67
+ [ Fact ]
68
+ public async Task InvokeValidationBehavior_WhenNoValidator_ShouldInvokeNextBehavior ( )
69
+ {
70
+ // Arrange
71
+ var createTodoListCommand = new CreateTodoListCommand ( "Title" ) ;
72
+ var validationBehavior = new ValidationBehavior < CreateTodoListCommand , ErrorOr < int > > ( ) ;
73
+
74
+ var todoList = new TodoList { Title = createTodoListCommand . Title } ;
75
+ _mockNextBehavior . Invoke ( ) . Returns ( todoList . Id ) ;
76
+
77
+ // Act
78
+ var result = await validationBehavior . Handle ( createTodoListCommand , _mockNextBehavior , default ) ;
79
+
80
+ // Assert
81
+ result . IsError . Should ( ) . BeFalse ( ) ;
82
+ result . Value . Should ( ) . Be ( todoList . Id ) ;
83
+ }
84
+ }
0 commit comments