Skip to content

Commit c1b876a

Browse files
committed
Feat: Add assertions for service key presence in ServiceCollection
1 parent fbc2c22 commit c1b876a

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

src/CodeOfChaos.Testing.TUnit/CodeOfChaos.Testing.TUnit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
<ItemGroup>
3232
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
33+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.4" />
3334
<PackageReference Include="TUnit.Assertions" Version="0.19.86" />
3435
<PackageReference Include="TUnit.Core" Version="0.19.86" />
3536
</ItemGroup>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.Extensions.DependencyInjection;
5+
using TUnit.Assertions.AssertConditions;
6+
7+
namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class ContainsServiceKeyCondition<TServiceKey> : BaseAssertCondition<ServiceCollection> {
12+
// -----------------------------------------------------------------------------------------------------------------
13+
// Methods
14+
// -----------------------------------------------------------------------------------------------------------------
15+
protected override string GetExpectation() => $"to have a registered service \"{typeof(TServiceKey).Name}\"";
16+
17+
protected override ValueTask<AssertionResult> GetResult(ServiceCollection? actualValue, Exception? exception, AssertionMetadata assertionMetadata) {
18+
if (actualValue is null) return AssertionResult.Fail($"{nameof(ServiceCollection)} is null");
19+
return actualValue.Any(d => d.ServiceType == typeof(TServiceKey))
20+
? AssertionResult.Passed
21+
: FailWithMessage($"No service with type {typeof(TServiceKey).Name} has been registered.");
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.Extensions.DependencyInjection;
5+
using TUnit.Assertions.AssertConditions;
6+
7+
namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class DoesNotContainServiceKeyCondition<TServiceKey> : BaseAssertCondition<ServiceCollection> {
12+
// -----------------------------------------------------------------------------------------------------------------
13+
// Methods
14+
// -----------------------------------------------------------------------------------------------------------------
15+
protected override string GetExpectation() => $"to have a registered service \"{typeof(TServiceKey).Name}\"";
16+
17+
protected override ValueTask<AssertionResult> GetResult(ServiceCollection? actualValue, Exception? exception, AssertionMetadata assertionMetadata) {
18+
if (actualValue is null) return AssertionResult.Fail($"{nameof(ServiceCollection)} is null");
19+
return actualValue.All(d => d.ServiceType != typeof(TServiceKey))
20+
? AssertionResult.Passed
21+
: FailWithMessage($"No service with type {typeof(TServiceKey).Name} has been registered.");
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Testing.TUnit.Conditions.Library;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using System.Runtime.CompilerServices;
7+
using TUnit.Assertions.AssertConditions.Interfaces;
8+
using TUnit.Assertions.AssertionBuilders;
9+
10+
namespace CodeOfChaos.Testing.TUnit;
11+
// ---------------------------------------------------------------------------------------------------------------------
12+
// Code
13+
// ---------------------------------------------------------------------------------------------------------------------
14+
// ReSharper disable once InconsistentNaming
15+
public static class TUnitExtensionsServiceCollection {
16+
public static InvokableValueAssertionBuilder<ServiceCollection> ContainsServiceKey<TServiceKey>(this IValueSource<ServiceCollection> valueSource, [CallerArgumentExpression(nameof(valueSource))] string doNotPopulateThisValue1 = "") {
17+
return valueSource.RegisterAssertion(
18+
new ContainsServiceKeyCondition<TServiceKey>(),
19+
[doNotPopulateThisValue1]
20+
);
21+
}
22+
23+
public static InvokableValueAssertionBuilder<ServiceCollection> DoesNotContainServiceKey<TServiceKey>(this IValueSource<ServiceCollection> valueSource, [CallerArgumentExpression(nameof(valueSource))] string doNotPopulateThisValue1 = "") {
24+
return valueSource.RegisterAssertion(
25+
new DoesNotContainServiceKeyCondition<TServiceKey>(),
26+
[doNotPopulateThisValue1]
27+
);
28+
}
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Testing.TUnit;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using TUnit.Assertions.Exceptions;
7+
8+
namespace Tests.CodeOfChaos.Testing.TUnit;
9+
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
// Code
12+
// ---------------------------------------------------------------------------------------------------------------------
13+
public class TUnitExtensionsServiceCollectionTests {
14+
[Test]
15+
public async Task ContainsServiceKey_ShouldAssert() {
16+
// Arrange
17+
var services = new ServiceCollection();
18+
19+
// Act
20+
services.AddSingleton<string, string>();
21+
22+
// Assert
23+
await Assert.That(services).ContainsServiceKey<string>();
24+
}
25+
26+
[Test]
27+
public async Task ContainsServiceKey_ShouldThrowAssertion() {
28+
// Arrange
29+
var services = new ServiceCollection();
30+
31+
// Act
32+
services.AddSingleton<string, string>();
33+
34+
// Assert
35+
await Assert.ThrowsAsync<AssertionException>(async () => await Assert.That(services).ContainsServiceKey<int>());
36+
}
37+
38+
[Test]
39+
public async Task DoesNotContainServiceKey_ShouldAssert() {
40+
// Arrange
41+
var services = new ServiceCollection();
42+
43+
// Act
44+
services.AddSingleton<string, string>();
45+
46+
// Assert
47+
await Assert.That(services).DoesNotContainServiceKey<int>();
48+
}
49+
50+
[Test]
51+
public async Task DoesNotContainServiceKey_ShouldThrowAssertion() {
52+
// Arrange
53+
var services = new ServiceCollection();
54+
55+
// Act
56+
services.AddSingleton<string, string>();
57+
58+
// Assert
59+
await Assert.ThrowsAsync<AssertionException>(async () => await Assert.That(services).DoesNotContainServiceKey<string>());
60+
}
61+
}

0 commit comments

Comments
 (0)