0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-07-03 03:52:56 +00:00
CleanArchitecture/CleanArchitecture.Domain.Tests/ValidationTestBase.cs
2023-03-06 15:51:24 +01:00

71 lines
1.9 KiB
C#

using CleanArchitecture.Domain.Commands;
using FluentAssertions;
using FluentValidation;
namespace CleanArchitecture.Domain.Tests;
public class ValidationTestBase<TCommand, TValidation>
where TCommand : CommandBase
where TValidation: AbstractValidator<TCommand>
{
protected readonly TValidation _validation;
protected ValidationTestBase(TValidation validation)
{
_validation = validation;
}
protected void ShouldBeValid(TCommand command)
{
var result = _validation.Validate(command);
result.IsValid.Should().BeTrue();
result.Errors.Should().BeEmpty();
}
protected void ShouldHaveSingleError(
TCommand command,
string expectedCode)
{
var result = _validation.Validate(command);
result.IsValid.Should().BeFalse();
result.Errors.Count.Should().Be(1);
result.Errors.First().ErrorCode.Should().Be(expectedCode);
}
protected void ShouldHaveSingleError(
TCommand command,
string expectedCode,
string expectedMessage)
{
var result = _validation.Validate(command);
result.IsValid.Should().BeFalse();
result.Errors.Count.Should().Be(1);
result.Errors.First().ErrorCode.Should().Be(expectedCode);
result.Errors.First().ErrorMessage.Should().Be(expectedMessage);
}
protected void ShouldHaveExpectedErrors(
TCommand command,
params KeyValuePair<string, string>[] expectedErrors)
{
var result = _validation.Validate(command);
result.IsValid.Should().BeFalse();
result.Errors.Count.Should().Be(expectedErrors.Length);
foreach (var error in expectedErrors)
{
result.Errors
.Count(validation => validation.ErrorCode == error.Key && validation.ErrorMessage == error.Value)
.Should()
.Be(1);
}
}
}