-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUnitTestExExtensions.Validation.cs
More file actions
71 lines (64 loc) · 4.82 KB
/
Copy pathUnitTestExExtensions.Validation.cs
File metadata and controls
71 lines (64 loc) · 4.82 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
68
69
70
71
#pragma warning disable IDE0130 // Namespace does not match folder structure; by design.
namespace UnitTestEx;
#pragma warning restore IDE0130 // Namespace does not match folder structure
public static partial class UnitTestExExtensions
{
/// <summary>
/// Executes the validation and asserts that it is successful (i.e. no errors).
/// </summary>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="validator">The <see cref="IValidator{T}"/>.</param>
/// <param name="value">The value to validate.</param>
/// <returns>The <see cref="IValidationResult{T}"/>.</returns>
/// <remarks>This is using <see cref="AwesomeAssertions"/> to assert that the validation is successful (i.e. <see cref="IValidationResult.HasErrors"/> is <see langword="false"/>).</remarks>
public static void AssertSuccess<TValue>(this IValidator<TValue> validator, TValue value)
=> AssertSuccessAsync(validator, value).GetAwaiter().GetResult();
/// <summary>
/// Executes the validation and asserts that it is successful (i.e. no errors).
/// </summary>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="validator">The <see cref="IValidator{T}"/>.</param>
/// <param name="value">The value to validate.</param>
/// <returns>The <see cref="IValidationResult{T}"/>.</returns>
/// <remarks>This is using <see cref="AwesomeAssertions"/> to assert that the validation is successful (i.e. <see cref="IValidationResult.HasErrors"/> is <see langword="false"/>).</remarks>
public static async Task<IValidationResult<TValue>> AssertSuccessAsync<TValue>(this IValidator<TValue> validator, TValue value)
{
var vr = await validator.ValidateAsync(value).ConfigureAwait(false);
vr.HasErrors.Should().BeFalse();
return vr;
}
/// <summary>
/// Executes the validation and asserts that it has errors and that the expected errors are present.
/// </summary>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="validator">The <see cref="IValidator{T}"/>.</param>
/// <param name="value">The value to validate.</param>
/// <param name="expectedErrors">The expected errors.</param>
/// <returns>The <see cref="IValidationResult{T}"/>.</returns>
/// <remarks>This is using <see cref="AwesomeAssertions"/> to assert that the validation was unsuccessful (i.e. <see cref="IValidationResult.HasErrors"/> is <see langword="true"/>), then comparing the
/// <paramref name="expectedErrors"/> with the <see cref="IValidationResult.Messages"/> using <see cref="Assertor.TryAreErrorsMatched(IEnumerable{ApiError}?, IEnumerable{ApiError}?, out string?)"/>.</remarks>
public static IValidationResult<TValue> AssertErrors<TValue>(this IValidator<TValue> validator, TValue value, params IEnumerable<ApiError> expectedErrors)
=> AssertErrorsAsync(validator, value, expectedErrors).GetAwaiter().GetResult();
/// <summary>
/// Executes the validation and asserts that it has errors and that the expected errors are present.
/// </summary>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="validator">The <see cref="IValidator{T}"/>.</param>
/// <param name="value">The value to validate.</param>
/// <param name="expectedErrors">The expected errors.</param>
/// <returns>The <see cref="IValidationResult{T}"/>.</returns>
/// <remarks>This is using <see cref="AwesomeAssertions"/> to assert that the validation was unsuccessful (i.e. <see cref="IValidationResult.HasErrors"/> is <see langword="true"/>), then comparing the
/// <paramref name="expectedErrors"/> with the <see cref="IValidationResult.Messages"/> using <see cref="Assertor.TryAreErrorsMatched(IEnumerable{ApiError}?, IEnumerable{ApiError}?, out string?)"/>.</remarks>
public static async Task<IValidationResult<TValue>> AssertErrorsAsync<TValue>(this IValidator<TValue> validator, TValue value, params IEnumerable<ApiError> expectedErrors)
{
if (expectedErrors == null || !expectedErrors.Any())
throw new ArgumentException($"At least one expected error must be provided; alternatively, use {nameof(AssertSuccess)}/{nameof(AssertSuccessAsync)} where asserting a successful validation).", nameof(expectedErrors));
var vr = await validator.ValidateAsync(value).ConfigureAwait(false);
vr.HasErrors.Should().BeTrue();
vr.Messages.Should().NotBeNull().And.HaveCountGreaterThan(0);
var actualErrors = vr.Messages.Where(x => x.Type == CoreEx.Entities.MessageType.Error).Select(x => new ApiError(x.Property, x.Text?.ToString() ?? "none")).ToArray();
if (!Assertor.TryAreErrorsMatched(expectedErrors, actualErrors, out var errorMessage))
false.Should().BeTrue(because: errorMessage);
return vr;
}
}