-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUnitTestExExtensions.cs
More file actions
90 lines (85 loc) · 5.91 KB
/
Copy pathUnitTestExExtensions.cs
File metadata and controls
90 lines (85 loc) · 5.91 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
#pragma warning disable IDE0130 // Namespace does not match folder structure; by design.
namespace UnitTestEx;
#pragma warning restore IDE0130 // Namespace does not match folder structure
/// <summary>
/// Provides extensions for <see cref="TesterBase"/> to support common testing scenarios.
/// </summary>
public static partial class UnitTestExExtensions
{
private static readonly ConcurrentDictionary<string, byte> _connectionStrings = [];
/// <summary>
/// Enables an <see cref="ExecutionContext"/> instance to be tested managed within a <see cref="TesterBase.Services"/> <see cref="ServiceProviderServiceExtensions.CreateScope(IServiceProvider)"/>.
/// </summary>
/// <typeparam name="TSelf">The <see cref="TesterBase{TSelf}"/> <see cref="Type"/>.</typeparam>
/// <param name="tester">The <see cref="TesterBase{TSelf}"/>.</param>
/// <param name="scopedTester">The <see cref="ScopedTypeTester{TService}"/> testing action.</param>
/// <param name="configure">The optional pre-test <see cref="ExecutionContext"/> configuration action.</param>
/// <returns>The <see cref="TesterBase{TSelf}"/> to support fluent-style method-chaining.</returns>
/// <remarks>This is a convenience method for common testing with an <see cref="ExecutionContext"/> instance within a scoped service lifetime.</remarks>
public static TSelf Scoped<TSelf>(this TesterBase<TSelf> tester, Action<ScopedTypeTester<ExecutionContext>> scopedTester, Action<ExecutionContext>? configure = null) where TSelf : TesterBase<TSelf>
{
tester.ThrowIfNull();
scopedTester.ThrowIfNull();
return tester.ScopedType<ExecutionContext>(test =>
{
configure?.Invoke(test.Service);
scopedTester(test);
});
}
/// <summary>
/// Enables an <see cref="ExecutionContext"/> instance to be tested managed within a <see cref="TesterBase.Services"/> <see cref="ServiceProviderServiceExtensions.CreateScope(IServiceProvider)"/>.
/// </summary>
/// <typeparam name="TSelf">The <see cref="TesterBase{TSelf}"/> <see cref="Type"/>.</typeparam>
/// <param name="tester">The <see cref="TesterBase{TSelf}"/>.</param>
/// <param name="scopedTester">The <see cref="ScopedTypeTester{TService}"/> testing action.</param>
/// <param name="configure">The optional pre-test <see cref="ExecutionContext"/> configuration action.</param>
/// <returns>The <see cref="TesterBase{TSelf}"/> to support fluent-style method-chaining.</returns>
/// <remarks>This is a convenience method for common testing with an <see cref="ExecutionContext"/> instance within a scoped service lifetime.</remarks>
public static TSelf Scoped<TSelf>(this TesterBase<TSelf> tester, Func<ScopedTypeTester<ExecutionContext>, Task> scopedTester, Action<ExecutionContext>? configure = null) where TSelf : TesterBase<TSelf>
{
tester.ThrowIfNull();
scopedTester.ThrowIfNull();
return tester.ScopedType<ExecutionContext>(async test =>
{
configure?.Invoke(test.Service);
await scopedTester(test).ConfigureAwait(false);
});
}
/// <summary>
/// Enables an <see cref="ExecutionContext"/> instance to be tested managed within a <see cref="TesterBase.Services"/> <see cref="ServiceProviderServiceExtensions.CreateScope(IServiceProvider)"/>.
/// </summary>
/// <typeparam name="TSelf">The <see cref="TesterBase{TSelf}"/> <see cref="Type"/>.</typeparam>
/// <param name="tester">The <see cref="TesterBase{TSelf}"/>.</param>
/// <param name="tenantId">The tenant identifier to set on the <see cref="ExecutionContext"/>.</param>
/// <param name="scopedTester">The <see cref="ScopedTypeTester{TService}"/> testing action.</param>
/// <param name="configure">The optional pre-test <see cref="ExecutionContext"/> configuration action.</param>
/// <returns>The <see cref="TesterBase{TSelf}"/> to support fluent-style method-chaining.</returns>
/// <remarks>This is a convenience method for common testing with an <see cref="ExecutionContext"/> instance within a scoped service lifetime.</remarks>
public static TSelf Scoped<TSelf>(this TesterBase<TSelf> tester, string? tenantId, Action<ScopedTypeTester<ExecutionContext>> scopedTester, Action<ExecutionContext>? configure = null) where TSelf : TesterBase<TSelf>
=> Scoped(tester, scopedTester, ec =>
{
ec.TenantId = tenantId;
configure?.Invoke(ec);
});
/// <summary>
/// Enables an <see cref="ExecutionContext"/> instance to be tested managed within a <see cref="TesterBase.Services"/> <see cref="ServiceProviderServiceExtensions.CreateScope(IServiceProvider)"/>.
/// </summary>
/// <typeparam name="TSelf">The <see cref="TesterBase{TSelf}"/> <see cref="Type"/>.</typeparam>
/// <param name="tester">The <see cref="TesterBase{TSelf}"/>.</param>
/// <param name="tenantId">The tenant identifier to set on the <see cref="ExecutionContext"/>.</param>
/// <param name="scopedTester">The <see cref="ScopedTypeTester{TService}"/> testing action.</param>
/// <param name="configure">The optional pre-test <see cref="ExecutionContext"/> configuration action.</param>
/// <returns>The <see cref="TesterBase{TSelf}"/> to support fluent-style method-chaining.</returns>
/// <remarks>This is a convenience method for common testing with an <see cref="ExecutionContext"/> instance within a scoped service lifetime.</remarks>
public static TSelf Scoped<TSelf>(this TesterBase<TSelf> tester, string? tenantId, Func<ScopedTypeTester<ExecutionContext>, Task> scopedTester, Action<ExecutionContext>? configure = null) where TSelf : TesterBase<TSelf>
{
tester.ThrowIfNull();
scopedTester.ThrowIfNull();
return tester.ScopedType<ExecutionContext>(async test =>
{
test.Service.TenantId = tenantId;
configure?.Invoke(test.Service);
await scopedTester(test).ConfigureAwait(false);
});
}
}