-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScopedDisposalTests.cs
39 lines (31 loc) · 1.14 KB
/
ScopedDisposalTests.cs
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
namespace DependencyInjection.Tests;
public class ScopedDisposalTests
{
private interface IMyDisposableService : IDisposable
{
bool IsDisposed { get; }
}
private class MyDisposableService : IMyDisposableService
{
public bool IsDisposed { get; private set; }
public void Dispose()
{
IsDisposed = true;
}
}
[Fact]
public void ScopedServices_ShouldBeDisposed_WhenScopeIsDisposed()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<IMyDisposableService, MyDisposableService>();
var serviceProvider = serviceCollection.BuildServiceProvider();
IMyDisposableService? disposableService;
using (var scope = serviceProvider.CreateScope())
{
disposableService = scope.ServiceProvider.GetService<IMyDisposableService>();
Assert.NotNull(disposableService);
Assert.False(disposableService.IsDisposed, "Service should not be disposed yet.");
}
Assert.True(disposableService.IsDisposed, "Service should be disposed after the scope is disposed.");
}
}