Files
ColaFlow/colaflow-api/tests/ColaFlow.Application.Tests/Commands/CreateTask/CreateTaskCommandHandlerTests.cs
Yaojia Wang 3232b70ecc fix(backend): Fix unit test compilation errors
Updated all unit tests to match updated method signatures after ProjectManagement Module refactoring.

Changes:
- Added TenantId parameter to Project.Create() calls in all test files
- Added TenantId parameter to ProjectCreatedEvent constructor calls
- Added IHostEnvironment and ILogger mock parameters to IdentityDbContext in Identity tests
- Fixed all test files in ColaFlow.Domain.Tests, ColaFlow.Application.Tests, and ColaFlow.Modules.Identity.Infrastructure.Tests

All tests now compile successfully with 0 errors (10 analyzer warnings only).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 10:28:01 +01:00

122 lines
4.3 KiB
C#

using FluentAssertions;
using Moq;
using ColaFlow.Modules.ProjectManagement.Application.Commands.CreateTask;
using ColaFlow.Modules.ProjectManagement.Domain.Aggregates.ProjectAggregate;
using ColaFlow.Modules.ProjectManagement.Domain.Repositories;
using ColaFlow.Modules.ProjectManagement.Domain.ValueObjects;
using ColaFlow.Modules.ProjectManagement.Domain.Exceptions;
namespace ColaFlow.Application.Tests.Commands.CreateTask;
public class CreateTaskCommandHandlerTests
{
private readonly Mock<IProjectRepository> _projectRepositoryMock;
private readonly Mock<IUnitOfWork> _unitOfWorkMock;
private readonly CreateTaskCommandHandler _handler;
public CreateTaskCommandHandlerTests()
{
_projectRepositoryMock = new Mock<IProjectRepository>();
_unitOfWorkMock = new Mock<IUnitOfWork>();
_handler = new CreateTaskCommandHandler(_projectRepositoryMock.Object, _unitOfWorkMock.Object);
}
[Fact]
public async Task Should_Create_Task_Successfully()
{
// Arrange
var userId = UserId.Create();
var project = Project.Create(TenantId.Create(Guid.NewGuid()), "Test Project", "Description", "TST", userId);
var epic = project.CreateEpic("Test Epic", "Epic Description", userId);
var story = epic.CreateStory("Test Story", "Story Description", TaskPriority.Medium, userId);
var storyId = story.Id;
_projectRepositoryMock
.Setup(x => x.GetProjectWithStoryAsync(storyId, It.IsAny<CancellationToken>()))
.ReturnsAsync(project);
var command = new CreateTaskCommand
{
StoryId = storyId.Value,
Title = "New Task",
Description = "Task Description",
Priority = "High",
EstimatedHours = 4,
AssigneeId = userId.Value,
CreatedBy = userId.Value
};
// Act
var result = await _handler.Handle(command, CancellationToken.None);
// Assert
result.Should().NotBeNull();
result.Title.Should().Be("New Task");
result.Description.Should().Be("Task Description");
result.StoryId.Should().Be(storyId.Value);
result.Status.Should().Be("To Do");
result.Priority.Should().Be("High");
result.EstimatedHours.Should().Be(4);
result.AssigneeId.Should().Be(userId.Value);
story.Tasks.Should().ContainSingle();
_projectRepositoryMock.Verify(x => x.Update(project), Times.Once);
_unitOfWorkMock.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Should_Fail_When_Story_Not_Found()
{
// Arrange
var storyId = StoryId.Create();
_projectRepositoryMock
.Setup(x => x.GetProjectWithStoryAsync(storyId, It.IsAny<CancellationToken>()))
.ReturnsAsync((Project?)null);
var command = new CreateTaskCommand
{
StoryId = storyId.Value,
Title = "New Task",
Description = "Description",
Priority = "Medium",
CreatedBy = Guid.NewGuid()
};
// Act
Func<Task> act = async () => await _handler.Handle(command, CancellationToken.None);
// Assert
await act.Should().ThrowAsync<NotFoundException>()
.WithMessage("*Story*");
}
[Fact]
public async Task Should_Set_Default_Status_To_ToDo()
{
// Arrange
var userId = UserId.Create();
var project = Project.Create(TenantId.Create(Guid.NewGuid()), "Test Project", "Description", "TST", userId);
var epic = project.CreateEpic("Test Epic", "Epic Description", userId);
var story = epic.CreateStory("Test Story", "Story Description", TaskPriority.Medium, userId);
_projectRepositoryMock
.Setup(x => x.GetProjectWithStoryAsync(story.Id, It.IsAny<CancellationToken>()))
.ReturnsAsync(project);
var command = new CreateTaskCommand
{
StoryId = story.Id.Value,
Title = "New Task",
Description = "Description",
Priority = "Low",
CreatedBy = userId.Value
};
// Act
var result = await _handler.Handle(command, CancellationToken.None);
// Assert
result.Status.Should().Be("To Do");
}
}