-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdatePositionCommandHandlerTests.cs
More file actions
67 lines (59 loc) · 2.76 KB
/
UpdatePositionCommandHandlerTests.cs
File metadata and controls
67 lines (59 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
namespace TalentManagementAPI.Application.Tests.Positions
{
public class UpdatePositionCommandHandlerTests
{
private readonly Mock<IPositionRepositoryAsync> _repositoryMock = new();
private readonly Mock<IEventDispatcher> _eventDispatcherMock = new();
[Fact]
public async Task Handle_ShouldUpdatePosition()
{
var departmentId = Guid.NewGuid();
var salaryRangeId = Guid.NewGuid();
var command = new UpdatePositionCommand
{
Id = Guid.NewGuid(),
PositionTitle = "Updated",
PositionNumber = "POS-002",
PositionDescription = "Updated description",
DepartmentId = departmentId,
SalaryRangeId = salaryRangeId
};
var position = new Position
{
Id = command.Id,
PositionTitle = new PositionTitle("Old"),
PositionNumber = "POS-001",
PositionDescription = "Old desc",
DepartmentId = Guid.NewGuid(),
SalaryRangeId = Guid.NewGuid()
};
_repositoryMock.Setup(r => r.GetByIdAsync(command.Id)).ReturnsAsync(position);
var handler = new UpdatePositionCommand.UpdatePositionCommandHandler(
_repositoryMock.Object,
_eventDispatcherMock.Object);
var result = await handler.Handle(command, CancellationToken.None);
result.IsSuccess.Should().BeTrue();
position.PositionTitle.Value.Should().Be("Updated");
position.PositionNumber.Should().Be("POS-002");
position.PositionDescription.Should().Be("Updated description");
position.DepartmentId.Should().Be(departmentId);
position.SalaryRangeId.Should().Be(salaryRangeId);
_repositoryMock.Verify(r => r.UpdateAsync(position), Times.Once);
_eventDispatcherMock.Verify(s => s.PublishAsync(
It.Is<PositionChangedEvent>(e => e.PositionId == position.Id),
It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Handle_ShouldThrowWhenPositionMissing()
{
var command = new UpdatePositionCommand { Id = Guid.NewGuid() };
_repositoryMock.Setup(r => r.GetByIdAsync(command.Id)).ReturnsAsync((Position)null!);
var handler = new UpdatePositionCommand.UpdatePositionCommandHandler(
_repositoryMock.Object,
_eventDispatcherMock.Object);
await FluentActions.Awaiting(() => handler.Handle(command, CancellationToken.None))
.Should().ThrowAsync<ApiException>()
.WithMessage("Position Not Found.");
}
}
}