Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Persons/GetPersonsHandlerTests.cs
T

64 lines
2.1 KiB
C#
Raw Normal View History

using Heimdall.Application.Common;
using Heimdall.Application.Persons.Queries.GetPersons;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using NSubstitute;
using Microsoft.EntityFrameworkCore;
namespace Heimdall.Application.Tests.Handlers.Persons;
public class GetPersonsHandlerTests
{
[Fact]
public async Task Get_persons_should_return_all()
{
var persons = new List<Person>
{
new("Charlie", "family"),
new("Alice", "employee"),
new("Bob", "family"),
};
var context = Substitute.For<IApplicationDbContext>();
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>(persons));
var handler = new GetPersonsQueryHandler(context);
var result = await handler.Handle(new GetPersonsQuery(null), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Count.ShouldBe(3);
}
[Fact]
public async Task Get_persons_should_filter_by_category()
{
var persons = new List<Person>
{
new("Charlie", "family"),
new("Alice", "employee"),
new("Bob", "family"),
};
var context = Substitute.For<IApplicationDbContext>();
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>(persons));
var handler = new GetPersonsQueryHandler(context);
var result = await handler.Handle(new GetPersonsQuery("family"), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Count.ShouldBe(2);
}
[Fact]
public async Task Get_persons_empty_should_return_empty_list()
{
var context = Substitute.For<IApplicationDbContext>();
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
var handler = new GetPersonsQueryHandler(context);
var result = await handler.Handle(new GetPersonsQuery(null), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldBeEmpty();
}
}