-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipelineBehaviorTests.cs
More file actions
144 lines (118 loc) · 4.36 KB
/
Copy pathPipelineBehaviorTests.cs
File metadata and controls
144 lines (118 loc) · 4.36 KB
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
using FluentAssertions;
using Mediator.Behaviors;
using Mediator.Tests.TestData;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Mediator.Tests;
/// <summary>
/// Tests for Pipeline Behaviors.
/// </summary>
public class PipelineBehaviorTests : IDisposable
{
private ServiceProvider? _serviceProvider;
public void Dispose()
{
_serviceProvider?.Dispose();
TrackingBehavior<PingRequest, PongResponse>.Reset();
FirstBehavior<PingRequest, PongResponse>.Reset();
SecondBehavior<PingRequest, PongResponse>.Reset();
ShortCircuitBehavior<PingRequest, PongResponse>.Reset();
}
private IMediator CreateMediator(Action<MediatorServiceConfiguration>? configure = null)
{
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(PipelineBehaviorTests).Assembly);
configure?.Invoke(config);
});
_serviceProvider = services.BuildServiceProvider();
return _serviceProvider.GetRequiredService<IMediator>();
}
[Fact]
public async Task Behavior_ExecutesBeforeAndAfterHandler()
{
// Arrange
var mediator = CreateMediator(config =>
{
config.AddBehavior(typeof(TrackingBehavior<,>));
});
TrackingBehavior<PingRequest, PongResponse>.Reset();
// Act
await mediator.Send(new PingRequest("Test"));
// Assert
var log = TrackingBehavior<PingRequest, PongResponse>.ExecutionLog;
log.Should().HaveCount(2);
log[0].Should().StartWith("Before:");
log[1].Should().StartWith("After:");
}
[Fact]
public async Task MultipleBehaviors_ExecuteInCorrectOrder()
{
// Arrange
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(PipelineBehaviorTests).Assembly);
config.AddBehavior(typeof(FirstBehavior<,>));
config.AddBehavior(typeof(SecondBehavior<,>));
});
_serviceProvider = services.BuildServiceProvider();
var mediator = _serviceProvider.GetRequiredService<IMediator>();
FirstBehavior<PingRequest, PongResponse>.Reset();
SecondBehavior<PingRequest, PongResponse>.Reset();
// Act
await mediator.Send(new PingRequest("Test"));
// Assert
var firstLog = FirstBehavior<PingRequest, PongResponse>.ExecutionLog;
var secondLog = SecondBehavior<PingRequest, PongResponse>.ExecutionLog;
firstLog.Should().Contain("First:Before");
firstLog.Should().Contain("First:After");
secondLog.Should().Contain("Second:Before");
secondLog.Should().Contain("Second:After");
}
[Fact]
public async Task Behavior_CanShortCircuitPipeline()
{
// Arrange
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(PipelineBehaviorTests).Assembly);
config.AddBehavior(typeof(ShortCircuitBehavior<,>));
});
_serviceProvider = services.BuildServiceProvider();
var mediator = _serviceProvider.GetRequiredService<IMediator>();
ShortCircuitBehavior<PingRequest, PongResponse>.ShouldShortCircuit = true;
ShortCircuitBehavior<PingRequest, PongResponse>.ShortCircuitResponse = new PongResponse("Short-circuited");
// Act
var response = await mediator.Send(new PingRequest("Original"));
// Assert
response.Reply.Should().Be("Short-circuited");
// Cleanup
ShortCircuitBehavior<PingRequest, PongResponse>.Reset();
}
[Fact]
public async Task Behavior_WithNoBehaviors_HandlerStillExecutes()
{
// Arrange
var mediator = CreateMediator();
// Act
var response = await mediator.Send(new PingRequest("No behaviors"));
// Assert
response.Reply.Should().Be("Pong: No behaviors");
}
[Fact]
public async Task Behavior_PassesRequestToHandler()
{
// Arrange
var mediator = CreateMediator(config =>
{
config.AddBehavior(typeof(TrackingBehavior<,>));
});
// Act
var response = await mediator.Send(new PingRequest("Passed through"));
// Assert
response.Reply.Should().Be("Pong: Passed through");
}
}