Skip to content

Commit c408efc

Browse files
committed
refactor: remove exceptions and tests
1 parent ee9ed55 commit c408efc

File tree

9 files changed

+90
-259
lines changed

9 files changed

+90
-259
lines changed

Directory.Packages.props

+1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
2929
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
3030
<PackageVersion Include="Moq" Version="4.16.1" />
31+
<PackageVersion Include="NSubstitute" Version="5.1.0" />
3132
</ItemGroup>
3233
</Project>

src/Api/Filters/ApiExceptionFilterAttribute.cs

-132
This file was deleted.

src/Application/Common/Exceptions/ForbiddenAccessException.cs

-19
This file was deleted.

src/Application/Common/Exceptions/NotFoundException.cs

-24
This file was deleted.

src/Application/Common/Exceptions/ValidationException.cs

-22
This file was deleted.

tests/Application.UnitTests/Application.UnitTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ItemGroup>
99
<PackageReference Include="FluentAssertions" />
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" />
11+
<PackageReference Include="NSubstitute" />
1112
<PackageReference Include="xunit" />
1213
<PackageReference Include="xunit.runner.visualstudio">
1314
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using FluentValidation;
2+
using FluentValidation.Results;
3+
4+
using MediatR;
5+
6+
using VerticalSliceArchitecture.Application.Common.Behaviours;
7+
using VerticalSliceArchitecture.Application.Domain.Todos;
8+
using VerticalSliceArchitecture.Application.Features.TodoLists;
9+
10+
namespace VerticalSliceArchitecture.Application.UnitTests.Common.Behaviours;
11+
12+
public class ValidationBehaviorTests
13+
{
14+
private readonly ValidationBehavior<CreateTodoListCommand, ErrorOr<int>> _validationBehavior;
15+
private readonly IValidator<CreateTodoListCommand> _mockValidator;
16+
private readonly RequestHandlerDelegate<ErrorOr<int>> _mockNextBehavior;
17+
18+
public ValidationBehaviorTests()
19+
{
20+
_mockNextBehavior = Substitute.For<RequestHandlerDelegate<ErrorOr<int>>>();
21+
_mockValidator = Substitute.For<IValidator<CreateTodoListCommand>>();
22+
23+
_validationBehavior = new(_mockValidator);
24+
}
25+
26+
[Fact]
27+
public async Task InvokeValidationBehavior_WhenValidatorResultIsValid_ShouldInvokeNextBehavior()
28+
{
29+
// Arrange
30+
var createTodoListCommand = new CreateTodoListCommand("Title");
31+
var todoList = new TodoList { Title = createTodoListCommand.Title };
32+
33+
_mockValidator
34+
.ValidateAsync(createTodoListCommand, Arg.Any<CancellationToken>())
35+
.Returns(new ValidationResult());
36+
37+
_mockNextBehavior.Invoke().Returns(todoList.Id);
38+
39+
// Act
40+
var result = await _validationBehavior.Handle(createTodoListCommand, _mockNextBehavior, default);
41+
42+
// Assert
43+
result.IsError.Should().BeFalse();
44+
result.Value.Should().Be(todoList.Id);
45+
}
46+
47+
[Fact]
48+
public async Task InvokeValidationBehavior_WhenValidatorResultIsNotValid_ShouldReturnListOfErrors()
49+
{
50+
// Arrange
51+
var createTodoListCommand = new CreateTodoListCommand("Title");
52+
List<ValidationFailure> validationFailures = [new(propertyName: "foo", errorMessage: "bad foo")];
53+
54+
_mockValidator
55+
.ValidateAsync(createTodoListCommand, Arg.Any<CancellationToken>())
56+
.Returns(new ValidationResult(validationFailures));
57+
58+
// Act
59+
var result = await _validationBehavior.Handle(createTodoListCommand, _mockNextBehavior, default);
60+
61+
// Assert
62+
result.IsError.Should().BeTrue();
63+
result.FirstError.Code.Should().Be("foo");
64+
result.FirstError.Description.Should().Be("bad foo");
65+
}
66+
67+
[Fact]
68+
public async Task InvokeValidationBehavior_WhenNoValidator_ShouldInvokeNextBehavior()
69+
{
70+
// Arrange
71+
var createTodoListCommand = new CreateTodoListCommand("Title");
72+
var validationBehavior = new ValidationBehavior<CreateTodoListCommand, ErrorOr<int>>();
73+
74+
var todoList = new TodoList { Title = createTodoListCommand.Title };
75+
_mockNextBehavior.Invoke().Returns(todoList.Id);
76+
77+
// Act
78+
var result = await validationBehavior.Handle(createTodoListCommand, _mockNextBehavior, default);
79+
80+
// Assert
81+
result.IsError.Should().BeFalse();
82+
result.Value.Should().Be(todoList.Id);
83+
}
84+
}

tests/Application.UnitTests/Common/Exceptions/ValidationExceptionTests.cs

-62
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
global using ErrorOr;
2+
13
global using FluentAssertions;
24

5+
global using NSubstitute;
6+
37
global using Xunit;

0 commit comments

Comments
 (0)