-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathContainerBuilderExtensionsTests.cs
221 lines (184 loc) · 8.62 KB
/
ContainerBuilderExtensionsTests.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using FluentAssertions;
using MediatR.Extensions.Autofac.DependencyInjection.Builder;
using MediatR.Extensions.Autofac.DependencyInjection.Tests.Behaviors;
using MediatR.Extensions.Autofac.DependencyInjection.Tests.Commands;
using MediatR.Extensions.Autofac.DependencyInjection.Tests.CustomTypes;
using MediatR.Extensions.Autofac.DependencyInjection.Tests.Handler;
using MediatR.Pipeline;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace MediatR.Extensions.Autofac.DependencyInjection.Tests;
public class ContainerBuilderExtensionsTests : IAsyncLifetime
{
private readonly ContainerBuilder builder;
private IContainer container;
public ContainerBuilderExtensionsTests()
{
this.builder = new ContainerBuilder();
}
[Fact]
public void RegisterMediatR_RegistrationScopeScoped_InstancesSameInScope()
{
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.WithAllOpenGenericHandlerTypesRegistered()
.WithRegistrationScope(RegistrationScope.Scoped)
.Build();
this.container = this.builder.RegisterMediatR(configuration).Build();
this.AssertServiceRegistered();
this.AssertServiceResolvable();
var mediatorOne = this.container.Resolve<IMediator>();
var mediatorTwo = this.container.Resolve<IMediator>();
mediatorOne.Should().BeSameAs(mediatorTwo);
}
[Fact]
public void RegisterMediatR_RegistrationScopeTransient_InstancesNotSameInScope()
{
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.WithAllOpenGenericHandlerTypesRegistered()
.WithRegistrationScope(RegistrationScope.Transient)
.Build();
this.container = this.builder.RegisterMediatR(configuration).Build();
this.AssertServiceRegistered();
this.AssertServiceResolvable();
var mediatorOne = this.container.Resolve<IMediator>();
var mediatorTwo = this.container.Resolve<IMediator>();
mediatorOne.Should().NotBeSameAs(mediatorTwo);
}
[Fact]
public void RegisterMediatR_CustomMediatorProvided_ExpectInstances()
{
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.UseMediatorType(typeof(CustomMediator))
.WithAllOpenGenericHandlerTypesRegistered()
.Build();
this.container = this.builder.RegisterMediatR(configuration).Build();
this.AssertServiceRegistered();
this.AssertServiceResolvable();
var publisher = this.container.Resolve<IMediator>();
publisher.Should().BeOfType<CustomMediator>();
}
[Fact]
public void RegisterMediatR_CustomPublisherProvided_ExpectInstances()
{
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.UseNotificationPublisher(typeof(CustomNotificationPublisher))
.WithAllOpenGenericHandlerTypesRegistered()
.Build();
this.container = this.builder.RegisterMediatR(configuration).Build();
this.AssertServiceRegistered();
this.AssertServiceResolvable();
var publisher = this.container.Resolve<INotificationPublisher>();
publisher.Should().BeOfType<CustomNotificationPublisher>();
}
[Fact]
public void RegisterMediatR_ConfigurationProvided_ExpectInstances()
{
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.WithAllOpenGenericHandlerTypesRegistered()
.Build();
this.container = this.builder.RegisterMediatR(configuration).Build();
this.AssertServiceRegistered();
this.AssertServiceResolvable();
}
[Fact]
public void RegisterMediatR_ServiceProviderNotProvided_WrapperResolved()
{
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.WithAllOpenGenericHandlerTypesRegistered()
.Build();
this.container = this.builder.RegisterMediatR(configuration).Build();
this.AssertServiceRegistered();
this.AssertServiceResolvable();
var serviceProvider = this.container.Resolve<IServiceProvider>();
serviceProvider.Should().BeOfType<ServiceProviderWrapper>();
}
[Fact]
public void RegisterMediatR_ServiceProviderProvidedFromOutside_AutofacServiceProviderResolved()
{
this.builder.Populate(new ServiceCollection());
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.WithAllOpenGenericHandlerTypesRegistered()
.Build();
this.container = this.builder.RegisterMediatR(configuration).Build();
this.AssertServiceRegistered();
this.AssertServiceResolvable();
var serviceProvider = this.container.Resolve<IServiceProvider>();
serviceProvider.Should().BeOfType<AutofacServiceProvider>();
}
[Fact]
public void RegisterMediatR_Manual_ExpectInstances()
{
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.WithRequestHandlersManuallyRegistered()
.Build();
this.builder
.RegisterMediatR(configuration)
.RegisterType<ResponseCommandHandler>()
.As<IRequestHandler<ResponseCommand, Response>>();
this.container = this.builder.Build();
Assert.True(this.container.IsRegistered<IRequestHandler<ResponseCommand, Response>>(), "Responsehandler not registered");
}
[Fact]
public void RegisterMediatR_ConfigurationProvidedWithCustomBehaviors_Resolvable()
{
var configuration = MediatRConfigurationBuilder
.Create(typeof(ResponseCommand).Assembly)
.WithCustomPipelineBehavior(typeof(LoggingBehavior<,>))
.WithAllOpenGenericHandlerTypesRegistered()
.Build();
this.container = this.builder.RegisterMediatR(configuration).Build();
this.AssertServiceRegistered();
this.AssertServiceResolvable();
this.container.IsRegistered(typeof(IPipelineBehavior<ResponseCommand, Response>));
var behaviors = this.container.Resolve<IEnumerable<IPipelineBehavior<ResponseCommand, Response>>>();
behaviors
.Select(type => type.GetType())
.Should()
.Contain(typeof(LoggingBehavior<ResponseCommand, Response>));
}
private void AssertServiceResolvable()
{
this.container.Resolve<IServiceProvider>();
this.container.Resolve<IMediator>();
this.container.Resolve<IRequestHandler<ResponseCommand, Response>>();
this.container.Resolve<IRequestHandler<VoidCommand>>();
this.container.Resolve<INotificationHandler<SampleNotification>>();
}
private void AssertServiceRegistered()
{
Assert.True(this.container.IsRegistered<IServiceProvider>(), "IServiceProvider not registered!");
Assert.True(this.container.IsRegistered<INotificationPublisher>(), "INotificationPublisher not registered!");
Assert.True(this.container.IsRegistered<IMediator>(), "Mediator not registered!");
Assert.True(this.container.IsRegistered<ISender>(), "ISender not registered!");
Assert.True(this.container.IsRegistered<IPublisher>(), "IPublisher not registered!");
Assert.True(this.container.IsRegistered<IPipelineBehavior<ResponseCommand, Response>>(), "PiplineBehavior not registered");
Assert.True(this.container.IsRegistered<IRequestHandler<ResponseCommand, Response>>(), "Responsehandler not registered");
Assert.True(this.container.IsRegistered<IRequestHandler<VoidCommand>>(), "Voidhandler not registered");
Assert.True(this.container.IsRegistered<IRequestPreProcessor<VoidCommand>>(), "Void Request Pre Processor not registered");
Assert.True(this.container.IsRegistered<IRequestPostProcessor<VoidCommand, Unit>>(), "Void Request Post Processor not registered");
Assert.True(this.container.IsRegistered<INotificationHandler<SampleNotification>>());
}
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
if (this.container is null)
{
return;
}
await this.container.DisposeAsync();
}
}