Working test using in-memory database
This commit is contained in:
parent
b80d7d0153
commit
274fe04ca2
4 changed files with 75 additions and 8 deletions
|
|
@ -32,11 +32,6 @@ public class ApplicationFactory<TProgram>
|
|||
{
|
||||
optionsBuilder.UseInMemoryDatabase("Test");
|
||||
});
|
||||
|
||||
services.AddDbContext<DatabaseContext>(optionsBuilder =>
|
||||
{
|
||||
optionsBuilder.UseInMemoryDatabase("Test");
|
||||
});
|
||||
#endregion
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,25 +2,27 @@ using Microsoft.AspNetCore.Mvc.Testing;
|
|||
|
||||
namespace LANCommander.Server.Tests;
|
||||
|
||||
public class ApplicationFixture : WebApplicationFactory<Program>
|
||||
public class ApplicationFixture : ApplicationFactory<Program>
|
||||
{
|
||||
public static ApplicationFixture Instance;
|
||||
|
||||
public SDK.Client Client { get; set; }
|
||||
public IServiceProvider ServiceProvider { get; set; }
|
||||
|
||||
public ApplicationFixture(WebApplicationFactory<Program> factory)
|
||||
public ApplicationFixture(ApplicationFactory<Program> factory)
|
||||
{
|
||||
if (Instance != null)
|
||||
return;
|
||||
|
||||
Client = new SDK.Client(factory.CreateClient(), "C:\\Games");
|
||||
ServiceProvider = factory.Services;
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
}
|
||||
|
||||
[CollectionDefinition("Application")]
|
||||
public class ApplicationCollection : ICollectionFixture<WebApplicationFactory<Program>>
|
||||
public class ApplicationCollection : ICollectionFixture<ApplicationFactory<Program>>
|
||||
{
|
||||
|
||||
}
|
||||
26
LANCommander.Server.Tests/BaseTest.cs
Normal file
26
LANCommander.Server.Tests/BaseTest.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
using System.Formats.Asn1;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace LANCommander.Server.Tests;
|
||||
|
||||
public abstract class BaseTest : IClassFixture<ApplicationFixture>, IDisposable
|
||||
{
|
||||
protected readonly SDK.Client Client = ApplicationFixture.Instance.Client;
|
||||
protected readonly IServiceProvider ServiceProvider;
|
||||
|
||||
private AsyncServiceScope? _scope;
|
||||
|
||||
public BaseTest(ApplicationFixture fixture)
|
||||
{
|
||||
_scope = ApplicationFixture.Instance.ServiceProvider.CreateAsyncScope();
|
||||
|
||||
ServiceProvider = _scope?.ServiceProvider;
|
||||
}
|
||||
|
||||
protected T GetService<T>() => ServiceProvider.GetService<T>();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_scope?.Dispose();
|
||||
}
|
||||
}
|
||||
44
LANCommander.Server.Tests/Services/UserServiceTests.cs
Normal file
44
LANCommander.Server.Tests/Services/UserServiceTests.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using LANCommander.Server.Data.Models;
|
||||
using LANCommander.Server.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Shouldly;
|
||||
|
||||
namespace LANCommander.Server.Tests.Services;
|
||||
|
||||
[Collection("Application")]
|
||||
public class UserServiceTests(ApplicationFixture fixture) : BaseTest(fixture)
|
||||
{
|
||||
[Fact]
|
||||
public async Task CreateAdminUserShouldWork()
|
||||
{
|
||||
var roleService = GetService<RoleService>();
|
||||
var userService = GetService<UserService>();
|
||||
|
||||
roleService.AddAsync(new Role
|
||||
{
|
||||
Name = RoleService.AdministratorRoleName,
|
||||
});
|
||||
|
||||
var user = await userService.AddAsync(new User
|
||||
{
|
||||
UserName = "admin",
|
||||
});
|
||||
|
||||
await userService.ChangePassword(user.UserName, "Password1234");
|
||||
await userService.AddToRoleAsync(user.UserName, RoleService.AdministratorRoleName);
|
||||
|
||||
user = await userService
|
||||
.Query(q =>
|
||||
{
|
||||
return q
|
||||
.Include(u => u.UserRoles)
|
||||
.ThenInclude(ur => ur.Role);
|
||||
})
|
||||
.FirstOrDefaultAsync(u => u.UserName.ToLower() == user.UserName.ToLower());
|
||||
|
||||
user.UserName.ShouldBe("admin");
|
||||
user.NormalizedUserName.ShouldBe("ADMIN");
|
||||
user.Roles.ShouldContain(r => r.Name == RoleService.AdministratorRoleName);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue