Refactor Fake class and enhance MockDbContext

Updated the `Fake` class to introduce flexible methods for generating fake `User` and `UserDto` objects with optional parameters. Removed the static `UserFaker` instance and related properties.

Added a new `DbSet<User>` property to `MockDbContext` for improved management of `User` entities.
This commit is contained in:
Developer 02 2025-04-17 14:47:37 +02:00
parent cf9041980d
commit 35050d65a8
2 changed files with 10 additions and 7 deletions

View File

@ -4,12 +4,13 @@ namespace DigitalData.Core.Tests.Mock;
public static class Fake
{
private static readonly Faker<User> UserFaker = new Faker<User>()
.RuleFor(u => u.FirstName, f => f.Name.FirstName())
.RuleFor(u => u.Email, f => f.Internet.Email())
.RuleFor(u => u.Age, f => f.Random.Int(18, 99));
public static Faker<User> CreateUserFaker(string? firstName = null, string? email = null, int? age = null) => new Faker<User>()
.RuleFor(u => u.FirstName, f => firstName ?? f.Name.FirstName())
.RuleFor(u => u.Email, f => email ?? f.Internet.Email())
.RuleFor(u => u.Age, f => age ?? f.Random.Int(18, 99));
public static User FakeUser => UserFaker.Generate();
public static List<User> CreateFakeUser(int count) => UserFaker.Generate(count);
public static Faker<UserDto> CreateUserDtoFaker(string? firstName = null, string? email = null, int? age = null) => new Faker<UserDto>()
.RuleFor(u => u.FirstName, f => firstName ?? f.Name.FirstName())
.RuleFor(u => u.Email, f => email ?? f.Internet.Email())
.RuleFor(u => u.Age, f => age ?? f.Random.Int(18, 99));
}

View File

@ -4,6 +4,8 @@ namespace DigitalData.Core.Tests.Mock;
public class MockDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public MockDbContext(DbContextOptions options) : base(options)
{
}