Test code that uses a repository (mock or fake)
At the end of this page, a unit test checks your code with a fake or a mock of the repository's interface, and no database.
Take the interface
The build generates an interface for the repository, IUserRepository, with its public methods. Write the code that uses the repository against the interface:
public sealed class UserGreeter(IUserRepository users)
{
public async Task<string> GreetAsync(int userId, CancellationToken cancellationToken = default)
{
var user = await users.GetUser(new GetUserParams(userId), cancellationToken).ConfigureAwait(false);
return GreetingOf(user);
}
private static string GreetingOf(GetUserDto? user) => user is null ? "Hello, stranger." : $"Hello, {user.Name}.";
}
Your application registers UserRepository as IUserRepository, as Call your queries shows. A test gives UserGreeter something else.
Write a fake
In your test project, which references the project of the repository, write a class that implements IUserRepository:
public sealed class FakeUserRepository : IUserRepository
{
public Dictionary<int, GetUserDto> Users { get; } = [];
public Task<GetUserDto?> GetUser(GetUserParams parameters, CancellationToken cancellationToken = default) =>
Task.FromResult(Users.GetValueOrDefault(parameters.UserId));
public Task<ListUserNamesDto[]> ListUserNames(CancellationToken cancellationToken = default) =>
Task.FromResult(Users.Values.Select(user => new ListUserNamesDto(user.Id, user.Name)).ToArray());
public Task<int> RenameUser(RenameUserParams parameters, CancellationToken cancellationToken = default) =>
throw new NotImplementedException();
public Task<FindUsersDto[]> FindUsers(FindUsersParams parameters, CancellationToken cancellationToken = default) =>
throw new NotImplementedException();
public Task<ListUsersByIdsDto[]> ListUsersByIds(
ListUsersByIdsParams parameters, CancellationToken cancellationToken = default) =>
throw new NotImplementedException();
public Task<ListDisplayNamesDto[]> ListDisplayNames(CancellationToken cancellationToken = default) =>
throw new NotImplementedException();
}
The fake needs a method for each query of UserRepository.sql. A method that your tests do not call can throw NotImplementedException. Then test with it:
[Fact]
public async Task Greet_KnownUser_SaysTheirName()
{
var users = new FakeUserRepository();
users.Users[1] = new GetUserDto(1, "Ada", "ada@example.com");
var greeting = await new UserGreeter(users).GreetAsync(1);
Assert.Equal("Hello, Ada.", greeting);
}
Run dotnet test. The test passes, and it needs no database.
Use a mocking library
A mocking library makes the fake for you. This test uses NSubstitute. Any mocking library works the same way, since IUserRepository is a plain interface:
[Fact]
public async Task Greet_KnownUser_SaysTheirName_WithAMock()
{
var users = Substitute.For<IUserRepository>();
users.GetUser(new GetUserParams(1), Arg.Any<CancellationToken>())
.Returns(new GetUserDto(1, "Ada", "ada@example.com"));
Assert.Equal("Hello, Ada.", await new UserGreeter(users).GreetAsync(1));
}
new GetUserParams(1) matches the call, since a record is equal to another record with the same values.
Add a member of your own
The interface is partial. To add a method of your own, declare it in a partial interface of the same name. Then write it in the class:
public partial interface IUserRepository
{
Task<int> CountUsers(CancellationToken cancellationToken = default);
}
public sealed partial class UserRepository
{
public async Task<int> CountUsers(CancellationToken cancellationToken = default) =>
(await ListUserNames(cancellationToken)).Length;
}
Your fake then needs CountUsers too.
If it does not work
- The test project cannot see
IUserRepository: the interface ispubliconly whenUserRepositoryis. Make the classpublic, or see Use SqlSource in several projects. - The fake does not compile after you add a query: add a method for the new query to the fake.
What you have now
Code that takes IUserRepository, and a test of it that uses a fake or a mock, with no database.
Rules: the interface, and how to rename or turn off the interface in repository interfaces.