Skip to content

Commit 845bef3

Browse files
Alexander Schützcmendible
authored andcommitted
added tests for sngleton and transient
1 parent 029a007 commit 845bef3

File tree

1 file changed

+140
-25
lines changed

1 file changed

+140
-25
lines changed

LazyProxy.ServiceProvider.Tests/ServiceCollectionExtensionsTest.cs

Lines changed: 140 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,33 @@ namespace LazyProxy.ServiceProvider.Tests
99

1010
public class ServiceCollectionExtensionsTest
1111
{
12-
[Fact]
13-
public void ServiceCtorMustBeExecutedAfterMethodIsCalledForTheFirstTime()
12+
13+
[InlineData(ServiceLifetime.Scoped)]
14+
[InlineData(ServiceLifetime.Singleton)]
15+
[InlineData(ServiceLifetime.Transient)]
16+
[Theory]
17+
public void ServiceCtorMustBeExecutedAfterMethodIsCalledForTheFirstTime(ServiceLifetime lifetime)
1418
{
1519
_service1Id = string.Empty;
1620
_service2Id = string.Empty;
1721

1822
var serviceCollection = new ServiceCollection();
19-
serviceCollection.AddLazyScoped<IService1, Service1>();
20-
serviceCollection.AddScoped<IService2, Service2>();
21-
23+
switch (lifetime)
24+
{
25+
case ServiceLifetime.Scoped:
26+
serviceCollection.AddLazyScoped<IService1, Service1>();
27+
serviceCollection.AddScoped<IService2, Service2>();
28+
break;
29+
case ServiceLifetime.Singleton:
30+
serviceCollection.AddLazySingleton<IService1, Service1>();
31+
serviceCollection.AddSingleton<IService2, Service2>();
32+
break;
33+
case ServiceLifetime.Transient:
34+
serviceCollection.AddLazyTransient<IService1, Service1>();
35+
serviceCollection.AddTransient<IService2, Service2>();
36+
break;
37+
}
38+
2239
using (var serviceProvider = serviceCollection.BuildServiceProvider())
2340
{
2441
var service = serviceProvider.GetService<IService1>();
@@ -43,15 +60,31 @@ public void ServiceCtorMustBeExecutedAfterMethodIsCalledForTheFirstTime()
4360
}
4461
}
4562

46-
[Fact]
47-
public void ServiceCtorMustBeExecutedAfterPropertyGetterIsCalledForTheFirstTime()
63+
[InlineData(ServiceLifetime.Scoped)]
64+
[InlineData(ServiceLifetime.Singleton)]
65+
[InlineData(ServiceLifetime.Transient)]
66+
[Theory]
67+
public void ServiceCtorMustBeExecutedAfterPropertyGetterIsCalledForTheFirstTime(ServiceLifetime lifetime)
4868
{
4969
_service1Id = string.Empty;
5070
_service2Id = string.Empty;
5171

5272
var serviceCollection = new ServiceCollection();
53-
serviceCollection.AddLazyScoped<IService1, Service1>();
54-
serviceCollection.AddScoped<IService2, Service2>();
73+
switch (lifetime)
74+
{
75+
case ServiceLifetime.Scoped:
76+
serviceCollection.AddLazyScoped<IService1, Service1>();
77+
serviceCollection.AddScoped<IService2, Service2>();
78+
break;
79+
case ServiceLifetime.Singleton:
80+
serviceCollection.AddLazySingleton<IService1, Service1>();
81+
serviceCollection.AddSingleton<IService2, Service2>();
82+
break;
83+
case ServiceLifetime.Transient:
84+
serviceCollection.AddLazyTransient<IService1, Service1>();
85+
serviceCollection.AddTransient<IService2, Service2>();
86+
break;
87+
}
5588

5689
using (var serviceProvider = serviceCollection.BuildServiceProvider())
5790
{
@@ -77,15 +110,31 @@ public void ServiceCtorMustBeExecutedAfterPropertyGetterIsCalledForTheFirstTime(
77110
}
78111
}
79112

80-
[Fact]
81-
public void ServiceCtorMustBeExecutedAfterPropertySetterIsCalledForTheFirstTime()
113+
[InlineData(ServiceLifetime.Scoped)]
114+
[InlineData(ServiceLifetime.Singleton)]
115+
[InlineData(ServiceLifetime.Transient)]
116+
[Theory]
117+
public void ServiceCtorMustBeExecutedAfterPropertySetterIsCalledForTheFirstTime(ServiceLifetime lifetime)
82118
{
83119
_service1Id = string.Empty;
84120
_service2Id = string.Empty;
85121

86122
var serviceCollection = new ServiceCollection();
87-
serviceCollection.AddLazyScoped<IService1, Service1>();
88-
serviceCollection.AddScoped<IService2, Service2>();
123+
switch (lifetime)
124+
{
125+
case ServiceLifetime.Scoped:
126+
serviceCollection.AddLazyScoped<IService1, Service1>();
127+
serviceCollection.AddScoped<IService2, Service2>();
128+
break;
129+
case ServiceLifetime.Singleton:
130+
serviceCollection.AddLazySingleton<IService1, Service1>();
131+
serviceCollection.AddSingleton<IService2, Service2>();
132+
break;
133+
case ServiceLifetime.Transient:
134+
serviceCollection.AddLazyTransient<IService1, Service1>();
135+
serviceCollection.AddTransient<IService2, Service2>();
136+
break;
137+
}
89138

90139
using (var serviceProvider = serviceCollection.BuildServiceProvider())
91140
{
@@ -109,15 +158,31 @@ public void ServiceCtorMustBeExecutedAfterPropertySetterIsCalledForTheFirstTime(
109158
}
110159
}
111160

112-
[Fact]
113-
public void ArgumentsMustBePassedToMethodCorrectly()
161+
[InlineData(ServiceLifetime.Scoped)]
162+
[InlineData(ServiceLifetime.Singleton)]
163+
[InlineData(ServiceLifetime.Transient)]
164+
[Theory]
165+
public void ArgumentsMustBePassedToMethodCorrectly(ServiceLifetime lifetime)
114166
{
115167
const string arg1 = nameof(arg1);
116168
const string arg2 = nameof(arg2);
117169

118170
var serviceCollection = new ServiceCollection();
119-
serviceCollection.AddLazyScoped<IService1, Service1>();
120-
serviceCollection.AddScoped<IService2, Service2>();
171+
switch (lifetime)
172+
{
173+
case ServiceLifetime.Scoped:
174+
serviceCollection.AddLazyScoped<IService1, Service1>();
175+
serviceCollection.AddScoped<IService2, Service2>();
176+
break;
177+
case ServiceLifetime.Singleton:
178+
serviceCollection.AddLazySingleton<IService1, Service1>();
179+
serviceCollection.AddSingleton<IService2, Service2>();
180+
break;
181+
case ServiceLifetime.Transient:
182+
serviceCollection.AddLazyTransient<IService1, Service1>();
183+
serviceCollection.AddTransient<IService2, Service2>();
184+
break;
185+
}
121186

122187
using (var serviceProvider = serviceCollection.BuildServiceProvider())
123188
{
@@ -131,11 +196,25 @@ public void ArgumentsMustBePassedToMethodCorrectly()
131196
}
132197
}
133198

134-
[Fact]
135-
public void DependencyResolutionExceptionMustBeThrownAfterMethodIsCalledWhenDependentServiceIsNotRegistered()
199+
[InlineData(ServiceLifetime.Scoped)]
200+
[InlineData(ServiceLifetime.Singleton)]
201+
[InlineData(ServiceLifetime.Transient)]
202+
[Theory]
203+
public void DependencyResolutionExceptionMustBeThrownAfterMethodIsCalledWhenDependentServiceIsNotRegistered(ServiceLifetime lifetime)
136204
{
137205
var serviceCollection = new ServiceCollection();
138-
serviceCollection.AddLazyScoped<IService1, Service1>();
206+
switch (lifetime)
207+
{
208+
case ServiceLifetime.Scoped:
209+
serviceCollection.AddLazyScoped<IService1, Service1>();
210+
break;
211+
case ServiceLifetime.Singleton:
212+
serviceCollection.AddLazySingleton<IService1, Service1>();
213+
break;
214+
case ServiceLifetime.Transient:
215+
serviceCollection.AddLazyTransient<IService1, Service1>();
216+
break;
217+
}
139218

140219
using (var serviceProvider = serviceCollection.BuildServiceProvider())
141220
{
@@ -145,11 +224,25 @@ public void DependencyResolutionExceptionMustBeThrownAfterMethodIsCalledWhenDepe
145224
}
146225
}
147226

148-
[Fact]
149-
public void InternalServiceMustBeResolvedFromAssemblyMarkedAsVisibleForProxy()
227+
[InlineData(ServiceLifetime.Scoped)]
228+
[InlineData(ServiceLifetime.Singleton)]
229+
[InlineData(ServiceLifetime.Transient)]
230+
[Theory]
231+
public void InternalServiceMustBeResolvedFromAssemblyMarkedAsVisibleForProxy(ServiceLifetime lifetime)
150232
{
151233
var serviceCollection = new ServiceCollection();
152-
serviceCollection.AddLazyScoped<IInternalService, InternalService>();
234+
switch (lifetime)
235+
{
236+
case ServiceLifetime.Scoped:
237+
serviceCollection.AddLazyScoped<IInternalService, InternalService>();
238+
break;
239+
case ServiceLifetime.Singleton:
240+
serviceCollection.AddLazySingleton<IInternalService, InternalService>();
241+
break;
242+
case ServiceLifetime.Transient:
243+
serviceCollection.AddLazyTransient<IInternalService, InternalService>();
244+
break;
245+
}
153246

154247
using (var serviceProvider = serviceCollection.BuildServiceProvider())
155248
{
@@ -160,10 +253,32 @@ public void InternalServiceMustBeResolvedFromAssemblyMarkedAsVisibleForProxy()
160253
}
161254
}
162255

163-
[Fact]
164-
public void ClosedGenericServiceMustBeResolved()
256+
[InlineData(ServiceLifetime.Scoped)]
257+
[InlineData(ServiceLifetime.Singleton)]
258+
[InlineData(ServiceLifetime.Transient)]
259+
[Theory]
260+
public void ClosedGenericServiceMustBeResolved(ServiceLifetime lifetime)
165261
{
166262
var serviceCollection = new ServiceCollection();
263+
switch (lifetime)
264+
{
265+
case ServiceLifetime.Scoped:
266+
serviceCollection.AddLazyScoped(
267+
typeof(IGenericService<ParameterType1, ParameterType2, ParameterType3>),
268+
typeof(GenericService<ParameterType1, ParameterType2, ParameterType3>));
269+
break;
270+
case ServiceLifetime.Singleton:
271+
serviceCollection.AddLazySingleton(
272+
typeof(IGenericService<ParameterType1, ParameterType2, ParameterType3>),
273+
typeof(GenericService<ParameterType1, ParameterType2, ParameterType3>));
274+
break;
275+
case ServiceLifetime.Transient:
276+
serviceCollection.AddLazyTransient(
277+
typeof(IGenericService<ParameterType1, ParameterType2, ParameterType3>),
278+
typeof(GenericService<ParameterType1, ParameterType2, ParameterType3>));
279+
break;
280+
}
281+
167282
serviceCollection.AddLazyScoped(
168283
typeof(IGenericService<ParameterType1, ParameterType2, ParameterType3>),
169284
typeof(GenericService<ParameterType1, ParameterType2, ParameterType3>));

0 commit comments

Comments
 (0)