-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathValidationTests.cs
More file actions
70 lines (56 loc) · 1.81 KB
/
Copy pathValidationTests.cs
File metadata and controls
70 lines (56 loc) · 1.81 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
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace MediatR.Extensions.FluentValidation.AspNetCore.Tests
{
public class ValidationTests
{
private readonly ServiceProvider Services;
public readonly IMediator Mediator;
public ValidationTests()
{
ServiceCollection sc = new ServiceCollection();
var domainAssembly = typeof(ValidationTests).GetTypeInfo().Assembly;
// Add MediatR
sc.AddMediatR(domainAssembly);
//Add FluentValidation
sc.AddFluentValidation(new[] { domainAssembly });
Services = sc.BuildServiceProvider();
Mediator = Services.GetService<IMediator>()!;
Assert.NotNull(Mediator);
}
[Fact]
public async Task CheckForValidationPass()
{
await Mediator.Send(new Command { Discount = 1 });
}
[Fact]
public async Task CheckForValidationFailure()
{
await Assert.ThrowsAsync<ValidationException>(() => Mediator.Send(new Command { Discount = -1 }));
}
}
public class CommandValidation : AbstractValidator<Command>
{
public CommandValidation()
{
RuleFor(x => x.Discount)
.GreaterThanOrEqualTo(0)
.MustAsync(async (_, __) => await Task.Delay(0).ContinueWith(x => true));
}
}
public class Command : IRequest
{
public decimal Discount { get; set; }
}
public class CommandHandler : IRequestHandler<Command>
{
public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
{
return await Unit.Task;
}
}
}