Repository navigation
Expand file tree
/
Copy pathUnitTestExExtensions.WithAspireTester.cs
More file actions
55 lines (47 loc) · 2.63 KB
/
Copy pathUnitTestExExtensions.WithAspireTester.cs
File metadata and controls
55 lines (47 loc) · 2.63 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
#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 a shared <see cref="AspireTester{TAppHost}"/> to enable usage of the same underlying <see cref="AspireTesterBase{TAppHost, TSelf}"/> instance across multiple tests.
/// </summary>
/// <typeparam name="TAppHost">The AppHost <see cref="Type"/> (a <c>Projects.*</c> type generated for the AppHost's <c>ProjectReference</c>).</typeparam>
public abstract class WithAspireTester<TAppHost> : IAsyncDisposable where TAppHost : class
{
private AspireTester<TAppHost>? _aspireTester;
/// <summary>
/// Initializes a new instance of the <see cref="WithAspireTester{TAppHost}"/> class.
/// </summary>
/// <param name="createFactory">The optional function to create the <see cref="TestFrameworkImplementor"/> instance.</param>
public WithAspireTester(Func<TestFrameworkImplementor>? createFactory = null)
=> _aspireTester ??= AspireTester.Create<TAppHost>(createFactory).BeforeStart(app => OnBeforeStartAsync(app)).AfterStart(app => OnAfterStartAsync(app));
/// <summary>
/// Gets the underlying <see cref="AspireTester{TAppHost}"/> for testing.
/// </summary>
public AspireTester<TAppHost> Test => _aspireTester ?? throw new ObjectDisposedException(nameof(Test));
/// <summary>
/// Provides an opportunity to perform any pre-start logic before the <see cref="DistributedApplication"/> is started.
/// </summary>
/// <param name="app">The <see cref="DistributedApplication"/> instance that is being started.</param>
protected virtual Task OnBeforeStartAsync(DistributedApplication app) => Task.CompletedTask;
/// <summary>
/// Provides an opportunity to perform any post-start logic after the <see cref="DistributedApplication"/> has been started.
/// </summary>
/// <param name="app">The <see cref="DistributedApplication"/> instance that has been started.</param>
protected virtual Task OnAfterStartAsync(DistributedApplication app) => Task.CompletedTask;
/// <inheritdoc/>
public async ValueTask DisposeAsync()
{
await DisposeCoreAsync().ConfigureAwait(false);
GC.SuppressFinalize(this);
}
/// <summary>
/// Performs the core asynchronous disposal logic; override to extend disposal behavior in derived classes.
/// </summary>
protected virtual async ValueTask DisposeCoreAsync()
{
if (_aspireTester is null)
return;
await _aspireTester.DisposeAsync().ConfigureAwait(false);
_aspireTester = null;
}
}