forked from KoenZomers/TadoApi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTest.cs
More file actions
56 lines (46 loc) · 1.79 KB
/
Copy pathBaseTest.cs
File metadata and controls
56 lines (46 loc) · 1.79 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
using KoenZomers.Tado.Api.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace KoenZomers.Tado.Api;
/// <summary>
/// Base functionality shared by all Unit Tests
/// </summary>
public abstract class IntegrationTestBase
{
/// <summary>
/// The service provider for the application
/// </summary>
protected readonly IServiceProvider? ServiceProvider;
/// <summary>
/// Access to the service instance
/// </summary>
protected readonly Api.Controllers.Tado? Service;
/// <summary>
/// Access to the service configuration
/// </summary>
protected readonly Api.Configuration.Tado? Configuration;
/// <summary>
/// Instantiate the Unit Test by creating the service provider and retrieving the service instance to be tested
/// </summary>
protected IntegrationTestBase()
{
var services = new ServiceCollection();
// Ensure the configuration is present
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.test.json", optional: true, reloadOnChange: true)
.Build();
services.AddSingleton<IConfiguration>(config);
// Add the complete service stack
Configuration = services.AddTadoServices();
ServiceProvider = services.BuildServiceProvider();
// Retrieve the requested service type to be tested
Service = ServiceProvider.GetService<Api.Controllers.Tado>();
// Ensure the service is present
if (Service == null)
{
throw new InvalidOperationException($"Failed to create service");
}
}
}