-
Notifications
You must be signed in to change notification settings - Fork 65
/
TestHost.cs
69 lines (56 loc) · 2.67 KB
/
TestHost.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
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
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Moq;
using MyWarehouse.Application.Dependencies.Services;
using System.IO;
using System.Reflection;
namespace MyWarehouse.Application.IntegrationTests
{
public class TestHost
{
public string ConnectionString { get; private set; }
public IWebHostEnvironment Environment { get; private set; }
public IServiceScopeFactory ScopeFactory { get; private set; }
public string CurrentUserId => FakeCurrentUserService.DefaultUserId;
private TestHost() { }
public static TestHost Create() => new TestHost().SetUpHost();
private TestHost SetUpHost()
{
Environment = Mock.Of<IWebHostEnvironment>(w =>
w.EnvironmentName == "Development" &&
w.ApplicationName == "MyWarehouse.Api");
// Last added overrides. Make sure .Testing.json is placed after API jsons.
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddUserSecrets(Assembly.Load(new AssemblyName(Environment.ApplicationName)), optional: true)
.AddJsonFile("appsettings.Testing.json", true, true)
.AddEnvironmentVariables()
.Build();
var services = new ServiceCollection()
.AddSingleton(Environment)
.AddLogging();
new MyWarehouse.Startup(configuration, Environment)
.ConfigureServices(services);
services = ReplaceServices(services);
ScopeFactory = services.BuildServiceProvider().GetService<IServiceScopeFactory>();
ConnectionString = configuration.GetConnectionString("DefaultConnection");
return this;
}
// Anticipating the need to replace multiple services in near future.
private static IServiceCollection ReplaceServices(IServiceCollection services)
{
services.Replace(ServiceDescriptor.Transient<ICurrentUserService, FakeCurrentUserService>());
return services;
}
private class FakeCurrentUserService : ICurrentUserService
{
public static readonly string DefaultUserId = "Test";
public string UserId => DefaultUserId;
}
}
}