Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 001ab96

Browse files
committed
add methods for testing
- `DisableStartupFilters`, `DisableStartupFilter<TImplementationFilter>` : Remove service registrations of `IStartFilter` - `RemoveSingleBy`, `RemoveAllBy`, `Remove`, `RemoveAll` : Remove service or implementation types. - `VerifyNoRegistrationByCondition`, `VerifyNoRegistration` : Verify there are no registration of condition or service type.
1 parent 59d5b08 commit 001ab96

File tree

11 files changed

+465
-39
lines changed

11 files changed

+465
-39
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ usign var client = new SystemUnderTest<Startup>()
259259
await client.DoSomethingAsync(new { Property = 1 });
260260
```
261261

262-
### To be documented
262+
### More methods.
263263
- `ReplaceConfigureOptions` : Replace registered configure options. if you used custom configure some option class in startup class.
264264
- `ReplaceNamedConfigureOptions` : Like above but just for named options.
265265
- `DisableOptionValidations` : Remove all options validators about `TOptions`
@@ -268,7 +268,9 @@ await client.DoSomethingAsync(new { Property = 1 });
268268
- `OverrideAppConfiguration` overloads
269269
- `ReplaceLoggerFactory` : Replace logger factory to generating custom logger.
270270
- for example : [checkout XUnit test sample](src/Wd3w.AspNetCore.EasyTesting.Test/SystemUnderTest/ReplaceLoggerFactoryTest.cs)
271-
271+
- `DisableStartupFilters`, `DisableStartupFilter<TImplementationFilter>` : Remove service registrations of `IStartFilter`
272+
- `RemoveSingleBy`, `RemoveAllBy`, `Remove`, `RemoveAll` : Remove service or implementation types.
273+
- `VerifyNoRegistrationByCondition`, `VerifyNoRegistration` : Verify there are no registration of condition or service type.
272274

273275
### And more use cases..
274276

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Wd3w.AspNetCore.EasyTesting.Test.Common
2+
{
3+
public interface INeverRegisteredServiceType
4+
{
5+
}
6+
7+
public class NeverRegisteredServiceType : INeverRegisteredServiceType
8+
{
9+
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Wd3w.AspNetCore.EasyTesting.Test.Common;
3+
using Xunit;
4+
5+
namespace Wd3w.AspNetCore.EasyTesting.Test.SystemUnderTest
6+
{
7+
public class DisableStartupFiltersTest : EasyTestingTestBase
8+
{
9+
[Fact]
10+
public void Should_RemoveIStartFilterRegistration_When_CallDisableStartupFilters()
11+
{
12+
// Given
13+
SUT.DisableStartupFilters();
14+
15+
// When
16+
SUT.CreateClient();
17+
18+
// Then
19+
SUT.VerifyNoRegistration<IStartupFilter>();
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Wd3w.AspNetCore.EasyTesting.SampleApi.Services;
2+
using Wd3w.AspNetCore.EasyTesting.Test.Common;
3+
using Xunit;
4+
5+
namespace Wd3w.AspNetCore.EasyTesting.Test.SystemUnderTest
6+
{
7+
public class RemoveAllByTest : EasyTestingTestBase
8+
{
9+
[Fact]
10+
public void Should_RemoveAllRegistration_When_ConditionIsDetermineSampleService()
11+
{
12+
// Given
13+
SUT.RemoveAllBy(descriptor => descriptor.ServiceType == typeof(ISampleService));
14+
15+
// When
16+
SUT.CreateClient();
17+
18+
// Then
19+
SUT.VerifyNoRegistration<ISampleService>();
20+
}
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Wd3w.AspNetCore.EasyTesting.SampleApi.Services;
2+
using Wd3w.AspNetCore.EasyTesting.Test.Common;
3+
using Xunit;
4+
5+
namespace Wd3w.AspNetCore.EasyTesting.Test.SystemUnderTest
6+
{
7+
public class RemoveAllTest : EasyTestingTestBase
8+
{
9+
[Fact]
10+
public void Should_DontBeRegisteredSampleService_When_RemoveISampleServiceTypeAsGeneric()
11+
{
12+
// Given
13+
SUT.RemoveAll<ISampleService>();
14+
15+
// When
16+
SUT.CreateClient();
17+
18+
// Then
19+
SUT.VerifyNoRegistration<ISampleService>();
20+
}
21+
22+
[Fact]
23+
public void Should_DontBeRegisteredSampleService_When_RemoveISampleServiceTypeAsParameter()
24+
{
25+
// Given
26+
SUT.RemoveAll(typeof(ISampleService));
27+
28+
// When
29+
SUT.CreateClient();
30+
31+
// Then
32+
SUT.VerifyNoRegistration<ISampleService>();
33+
}
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Wd3w.AspNetCore.EasyTesting.SampleApi.Services;
2+
using Wd3w.AspNetCore.EasyTesting.Test.Common;
3+
using Xunit;
4+
5+
namespace Wd3w.AspNetCore.EasyTesting.Test.SystemUnderTest
6+
{
7+
public class RemoveSingleByTest : EasyTestingTestBase
8+
{
9+
[Fact]
10+
public void Should_RemoveAllRegistration_When_ConditionIsDetermineSampleService()
11+
{
12+
// Given
13+
SUT.RemoveSingleBy(descriptor => descriptor.ServiceType == typeof(ISampleService));
14+
15+
// When
16+
SUT.CreateClient();
17+
18+
// Then
19+
SUT.VerifyNoRegistration<ISampleService>();
20+
}
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Wd3w.AspNetCore.EasyTesting.SampleApi.Services;
2+
using Wd3w.AspNetCore.EasyTesting.Test.Common;
3+
using Xunit;
4+
5+
namespace Wd3w.AspNetCore.EasyTesting.Test.SystemUnderTest
6+
{
7+
public class RemoveTest : EasyTestingTestBase
8+
{
9+
[Fact]
10+
public void Should_DontBeRegisteredSampleService_When_RemoveISampleServiceTypeAsGeneric()
11+
{
12+
// Given
13+
SUT.Remove<ISampleService, SampleService>();
14+
15+
// When
16+
SUT.CreateClient();
17+
18+
// Then
19+
SUT.VerifyNoRegistration<ISampleService>();
20+
}
21+
22+
[Fact]
23+
public void Should_DontBeRegisteredSampleService_When_RemoveISampleServiceTypeAsParameter()
24+
{
25+
// Given
26+
SUT.Remove(typeof(ISampleService), typeof(SampleService));
27+
28+
// When
29+
SUT.CreateClient();
30+
31+
// Then
32+
SUT.VerifyNoRegistration<ISampleService>();
33+
}
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using FluentAssertions;
3+
using Wd3w.AspNetCore.EasyTesting.SampleApi.Services;
4+
using Wd3w.AspNetCore.EasyTesting.Test.Common;
5+
using Xunit;
6+
using Xunit.Sdk;
7+
8+
namespace Wd3w.AspNetCore.EasyTesting.Test.SystemUnderTest
9+
{
10+
public class VerifyNoRegistrationByConditionTest : EasyTestingTestBase
11+
{
12+
[Fact]
13+
public void Should_VerifyToSuccess_When_ServiceTypeNeverRegistered()
14+
{
15+
// Given
16+
// When
17+
SUT.CreateClient();
18+
19+
// Then
20+
SUT.VerifyNoRegistrationByCondition(descriptor => descriptor.ServiceType == typeof(INeverRegisteredServiceType));
21+
}
22+
23+
[Fact]
24+
public void Should_VerifyToFail_When_ServiceTypeNeverRegistered()
25+
{
26+
// Given
27+
// When
28+
SUT.CreateClient();
29+
Action verify = () => SUT.VerifyNoRegistrationByCondition(descriptor => descriptor.ServiceType == typeof(ISampleService));
30+
31+
// Then
32+
verify.Should().Throw<XunitException>();
33+
}
34+
}
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Wd3w.AspNetCore.EasyTesting.Test.Common;
2+
using Xunit;
3+
4+
namespace Wd3w.AspNetCore.EasyTesting.Test.SystemUnderTest
5+
{
6+
public class VerifyNoRegistrationTest : EasyTestingTestBase
7+
{
8+
[Fact]
9+
public void Should_Verify_With_OneServiceTypeGenericParameter()
10+
{
11+
// Given
12+
// When
13+
SUT.CreateClient();
14+
15+
// Then
16+
SUT.VerifyNoRegistration<INeverRegisteredServiceType>();
17+
}
18+
19+
[Fact]
20+
public void Should_Verify_With_OneServiceTypeInstanceParameter()
21+
{
22+
// Given
23+
// When
24+
SUT.CreateClient();
25+
26+
// Then
27+
SUT.VerifyNoRegistration(typeof(INeverRegisteredServiceType));
28+
}
29+
30+
[Fact]
31+
public void Should_Verify_With_TwoTypeParameters()
32+
{
33+
// Given
34+
// When
35+
SUT.CreateClient();
36+
37+
// Then
38+
SUT.VerifyNoRegistration<INeverRegisteredServiceType, NeverRegisteredServiceType>();
39+
}
40+
41+
[Fact]
42+
public void Should_Verify_With_TwoParameter()
43+
{
44+
// Given
45+
// When
46+
SUT.CreateClient();
47+
48+
// Then
49+
SUT.VerifyNoRegistration(typeof(INeverRegisteredServiceType), typeof(NeverRegisteredServiceType));
50+
}
51+
}
52+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using FluentAssertions;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Wd3w.AspNetCore.EasyTesting
7+
{
8+
public abstract partial class SystemUnderTest
9+
{
10+
/// <summary>
11+
/// Verify lifetime of registered service.
12+
/// </summary>
13+
/// <param name="lifetime"></param>
14+
/// <typeparam name="TService"></typeparam>
15+
/// <returns></returns>
16+
/// <exception cref="InvalidOperationException"></exception>
17+
public void VerifyRegisteredLifeTimeOfService<TService>(ServiceLifetime lifetime)
18+
{
19+
CheckServiceCollectionAllocated();
20+
FindServiceDescriptor<TService>().Lifetime.Should().Be(lifetime);
21+
}
22+
23+
/// <summary>
24+
/// Verify implementation type of registered service.
25+
/// </summary>
26+
/// <typeparam name="TService"></typeparam>
27+
/// <typeparam name="TImplementation"></typeparam>
28+
public void VerifyRegisteredImplementationTypeOfService<TService, TImplementation>()
29+
{
30+
CheckServiceCollectionAllocated();
31+
GetImplementationType(FindServiceDescriptor<TService>()).Should().Be(typeof(TImplementation));
32+
}
33+
34+
/// <summary>
35+
/// Verify service collection with custom condition expression
36+
/// </summary>
37+
/// <param name="condition"></param>
38+
public void VerifyRegistrationByCondition(Expression<Func<ServiceDescriptor, bool>> condition)
39+
{
40+
CheckServiceCollectionAllocated();
41+
_serviceCollection.Should().Contain(condition);
42+
}
43+
44+
/// <summary>
45+
/// Verify there are no service descriptor registration by condition expression.
46+
/// </summary>
47+
/// <param name="condition"></param>
48+
public void VerifyNoRegistrationByCondition(Expression<Func<ServiceDescriptor, bool>> condition)
49+
{
50+
CheckServiceCollectionAllocated();
51+
_serviceCollection.Should().NotContain(condition);
52+
}
53+
54+
/// <summary>
55+
/// Verify there are no service descriptor of TService type
56+
/// </summary>
57+
/// <typeparam name="TService"></typeparam>
58+
public void VerifyNoRegistration<TService>()
59+
{
60+
VerifyNoRegistration(typeof(TService));
61+
}
62+
63+
/// <summary>
64+
/// Verify there are no service descriptor of serviceType
65+
/// </summary>
66+
/// <param name="serviceType"></param>
67+
public void VerifyNoRegistration(Type serviceType)
68+
{
69+
CheckServiceCollectionAllocated();
70+
_serviceCollection.Should().NotContain(descriptor => descriptor.ServiceType == serviceType);
71+
}
72+
73+
/// <summary>
74+
/// Verify there are no service descriptor of TService type and TImplementation type
75+
/// </summary>
76+
/// <typeparam name="TService"></typeparam>
77+
/// <typeparam name="TImplementation"></typeparam>
78+
public void VerifyNoRegistration<TService, TImplementation>() where TImplementation : class, TService
79+
{
80+
VerifyNoRegistration(typeof(TService), typeof(TImplementation));
81+
}
82+
83+
84+
/// <summary>
85+
/// Verify there are no service descriptor of serviceType and implementationType
86+
/// </summary>
87+
/// <param name="serviceType"></param>
88+
/// <param name="implementationType"></param>
89+
public void VerifyNoRegistration(Type serviceType, Type implementationType)
90+
{
91+
CheckServiceCollectionAllocated();
92+
_serviceCollection.Should().NotContain(descriptor =>
93+
descriptor.ServiceType == serviceType && (descriptor.ImplementationType == implementationType ||
94+
descriptor.ImplementationInstance.GetType() == implementationType));
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)