-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLoggerBehaviorTests.cs
66 lines (57 loc) · 1.92 KB
/
LoggerBehaviorTests.cs
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
using Cnblogs.Architecture.Ddd.Cqrs.Abstractions;
using Cnblogs.Architecture.UnitTests.Cqrs.FakeObjects;
using Microsoft.Extensions.Logging;
using NSubstitute;
namespace Cnblogs.Architecture.UnitTests.Cqrs.Behaviors;
public class LoggerBehaviorTests
{
[Fact]
public async Task LoggerBehavior_ShouldLogDebugAsync()
{
// Arrange
var logger = Substitute.For<ILogger<LoggingBehavior<FakeQuery<string>, string>>>();
var behavior =
new LoggingBehavior<FakeQuery<string>, string>(
new TestLogger<LoggingBehavior<FakeQuery<string>, string>>(logger));
var request = new FakeQuery<string>(null, "test");
// Act
await behavior.Handle(request, _ => Task.FromResult("done"), CancellationToken.None);
// Assert
logger.Received(2).Log(
LogLevel.Debug,
Arg.Any<EventId>(),
Arg.Any<object>(),
null,
Arg.Any<Func<object, Exception?, string>>());
}
private class TestLogger<T> : ILogger<T>
{
private readonly ILogger<T> _logger;
// ReSharper disable once ContextualLoggerProblem
public TestLogger(ILogger<T> logger)
{
_logger = logger;
}
/// <inheritdoc />
public IDisposable? BeginScope<TState>(TState state)
where TState : notnull
{
return _logger.BeginScope(state);
}
/// <inheritdoc />
public virtual bool IsEnabled(LogLevel logLevel)
{
return _logger.IsEnabled(logLevel);
}
/// <inheritdoc />
public virtual void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter)
{
_logger.Log<object>(logLevel, eventId, state!, exception, (_, _) => string.Empty);
}
}
}