Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Groups/CreateGroupHandlerTests.cs
T

43 lines
1.6 KiB
C#
Raw Normal View History

using Heimdall.Application.Common;
using Heimdall.Application.Groups.Commands.CreateGroup;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using NSubstitute;
using Microsoft.EntityFrameworkCore;
namespace Heimdall.Application.Tests.Handlers.Groups;
public class CreateGroupHandlerTests
{
[Fact]
public async Task Create_group_should_return_success()
{
var context = Substitute.For<IApplicationDbContext>();
context.Groups.Returns(MockDbSetHelper.CreateFor<Group>());
context.AuditLogs.Returns(MockDbSetHelper.CreateFor<AuditLog>());
var handler = new CreateGroupCommandHandler(context);
var result = await handler.Handle(
new CreateGroupCommand("New Group", "A test group"), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Name.ShouldBe("New Group");
await context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Create_duplicate_group_name_should_return_conflict()
{
var existingGroup = new Group("Existing Group", null);
var context = Substitute.For<IApplicationDbContext>();
context.Groups.Returns(MockDbSetHelper.CreateFor<Group>([existingGroup]));
var handler = new CreateGroupCommandHandler(context);
var result = await handler.Handle(
new CreateGroupCommand("Existing Group", null), CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(409);
}
}