-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTestWebApplicationFactory.cs
82 lines (72 loc) · 3.36 KB
/
TestWebApplicationFactory.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
70
71
72
73
74
75
76
77
78
79
80
81
82
using BlogCoreAPI.Models.DTOs.Account;
using DBAccess;
using DBAccess.Data;
using DBAccess.DataContext;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Linq;
using BlogCoreAPI.FunctionalTests.Models;
using BlogCoreAPI.Services.MailService;
using Microsoft.Extensions.DependencyInjection.Extensions;
using User = DBAccess.Data.User;
namespace BlogCoreAPI.FunctionalTests
{
public class TestWebApplicationFactory : WebApplicationFactory<Program>
{
private readonly string _dbName = Guid.NewGuid().ToString();
public AccountLoginDto Admin { get; set; }
public AccountLoginDto Account { get; set; }
private static IConfiguration GetConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
IConfiguration config = builder.Build();
return config;
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(async services =>
{
var descriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<BlogCoreContext>));
if (descriptor != null)
{
services.Remove(descriptor);
}
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
services.AddDbContext<BlogCoreContext>(options =>
{
options.UseInMemoryDatabase(_dbName);
options.UseInternalServiceProvider(serviceProvider);
});
// Add AutoMapper profile
services.AddAutoMapper(typeof(AutoMapperProfile));
services.Replace(ServiceDescriptor.Scoped<IEmailService, EmailServiceMock>());
var sp = services.BuildServiceProvider();
using var scope = sp.CreateScope();
await using var context = scope.ServiceProvider.GetRequiredService<BlogCoreContext>();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<User>>();
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<Role>>();
await context.Database.EnsureCreatedAsync();
// Fill Db with data
await DbInitializer.SeedWithDefaultValues(context, roleManager, userManager);
var configuration = GetConfiguration();
var user = configuration.GetSection("Users").GetSection("User").Get<Models.User>();
Account = new AccountLoginDto() { UserName = user.Name, Password = user.Password };
var admin = configuration.GetSection("Users").GetSection("Admin").Get<Models.User>();
Admin = new AccountLoginDto() { UserName = admin.Name, Password = admin.Password };
});
}
}
}