Enhance IRepository and update DbRepositoryTests
- Added ReadAsync method to IRepository for user retrieval. - Introduced _userRepo field in DbRepositoryTests for better clarity. - Modified Setup method to initialize _userRepo from the service provider. - Updated CreateAsync_ShouldNotThrow to use _userRepo. - Added ReadAsync_ShouldReturnCreated test for user retrieval validation.
This commit is contained in:
parent
06df97597e
commit
9376fcff86
@ -17,4 +17,5 @@ public interface IRepository<TEntity>
|
|||||||
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task DeleteAsync<TDto>(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
public Task DeleteAsync<TDto>(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);
|
||||||
|
Task ReadAsync(Core.Tests.Mock.User user);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,9 @@ using DigitalData.Core.Abstractions.Infrastructure;
|
|||||||
public class DbRepositoryTests
|
public class DbRepositoryTests
|
||||||
{
|
{
|
||||||
private IHost _host;
|
private IHost _host;
|
||||||
|
|
||||||
|
private IRepository<User> _userRepo;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
{
|
{
|
||||||
@ -24,6 +26,8 @@ public class DbRepositoryTests
|
|||||||
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
||||||
|
|
||||||
_host = builder.Build();
|
_host = builder.Build();
|
||||||
|
|
||||||
|
_userRepo = _host.Services.GetRequiredService<IRepository<User>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[TearDown]
|
[TearDown]
|
||||||
@ -39,12 +43,27 @@ public class DbRepositoryTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var faker = Fake.CreateUserFaker();
|
var faker = Fake.CreateUserFaker();
|
||||||
var repo = _host.Services.GetRequiredService<IRepository<User>>();
|
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
if (multiple)
|
if (multiple)
|
||||||
Assert.DoesNotThrowAsync(async () => await repo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
|
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
|
||||||
else
|
else
|
||||||
Assert.DoesNotThrowAsync(async () => await repo.CreateAsync(faker.Generate()));
|
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase(true, TestName = "WhenGivenMultipleUsers")]
|
||||||
|
[TestCase(false, TestName = "WhenGivenSingleUser")]
|
||||||
|
public async Task ReadAsync_ShouldReturnCreated(bool multiple)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var faker = Fake.CreateUserFaker();
|
||||||
|
var user = faker.Generate();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await _userRepo.CreateAsync(user);
|
||||||
|
var createdUser = (await _userRepo.ReadAsync()).First();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(createdUser, Is.EqualTo(user));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user