arrange tests

This commit is contained in:
2025-09-11 18:53:39 +02:00
parent 8743325067
commit db8c41368d
7 changed files with 38 additions and 26 deletions

View File

@@ -5,15 +5,14 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;
using DigitalData.Core.Infrastructure.AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository;
using Bogus.Platform;
using DigitalData.Core.Infrastructure;
public class DbRepositoryTests
{
private IHost _host;
private IRepository<User> _userRepo;
private IRepository Repo;
[SetUp]
public void Setup()
@@ -22,13 +21,16 @@ public class DbRepositoryTests
builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB"));
builder.Services.AddDbRepository<MockDbContext>(typeof(User).GetAssembly());
builder.Services.AddDbRepository(opt =>
{
opt.RegisterFromAssembly<MockDbContext>(typeof(User).Assembly);
});
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
_host = builder.Build();
_userRepo = _host.Services.GetRequiredService<IRepository<User>>();
Repo = _host.Services.GetRequiredService<IRepository>();
}
[TearDown]
@@ -47,9 +49,9 @@ public class DbRepositoryTests
// Act & Assert
if (multiple)
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
else
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate()));
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate()));
}
[TestCase(true, TestName = "WhenDtoUsed")]
@@ -58,10 +60,10 @@ public class DbRepositoryTests
{
// Act
var createdUser = useDto
? await _userRepo.CreateAsync(Fake.UserCreateDto)
: await _userRepo.CreateAsync(Fake.User);
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
: await Repo.CreateAsync(Fake.User);
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Assert
Assert.Multiple(() =>
@@ -75,12 +77,12 @@ public class DbRepositoryTests
public async Task ReadAsync_ShouldReturnUpdated()
{
// Arrange
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto);
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
var userUpdateDto = new UserUpdateDto() { Age = 10, Email = "Bar", FirstName = "Foo" };
// Act
await _userRepo.UpdateAsync(userUpdateDto, u => u.Id == createdUser!.Id);
var upToDateUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser!.Id);
await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, u => u.Id == createdUser!.Id);
var upToDateUser = await Repo.Get<User>().Where(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
// Assert
Assert.Multiple(() =>
@@ -96,12 +98,12 @@ public class DbRepositoryTests
public async Task ReadAsync_ShouldNotReturnDeleted()
{
// Arrange
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto);
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Act
await _userRepo.DeleteAsync(u => u.Id == createdUser.Id);
var deletedUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
await Repo.DeleteAsync<User>(u => u.Id == createdUser.Id);
var deletedUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Assert
Assert.Multiple(() =>