-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoMockingFactsBase.cs
More file actions
32 lines (26 loc) · 1.01 KB
/
AutoMockingFactsBase.cs
File metadata and controls
32 lines (26 loc) · 1.01 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
using Microsoft.Extensions.DependencyInjection;
using Moq.AutoMock;
namespace AddressBook;
/// <summary>
/// Instantiates a test <typeparamref name="TSubject"/>, automatically injecting mocks for its dependencies.
/// </summary>
public abstract class AutoMockingFactsBase<TSubject> : AutoMocker, IDisposable
where TSubject : class
{
private readonly Lazy<TSubject> _subject;
/// <summary>
/// The system under test.
/// </summary>
protected TSubject Subject => _subject.Value;
protected AutoMockingFactsBase()
{
GetMock<IServiceProvider>().Setup(x => x.GetService(It.IsAny<Type>())).Returns<Type>(Get);
GetMock<IServiceScope>().SetupGet(x => x.ServiceProvider).Returns(Get<IServiceProvider>);
GetMock<IServiceScopeFactory>().Setup(x => x.CreateScope()).Returns(Get<IServiceScope>);
_subject = new Lazy<TSubject>(CreateInstance<TSubject>);
}
/// <summary>
/// Verifies all created mocks.
/// </summary>
public virtual void Dispose() => Verify();
}