// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using FakeItEasy; using Microsoft.Extensions.Logging; using Xunit; namespace Squidex.Infrastructure.Commands { public class LogCommandMiddlewareTests { private readonly ILogger log = A.Fake>(); private readonly LogCommandMiddleware sut; private readonly ICommand command = A.Dummy(); private readonly ICommandBus commandBus = A.Dummy(); public LogCommandMiddlewareTests() { A.CallTo(() => log.IsEnabled(A._)) .Returns(true); sut = new LogCommandMiddleware(log); } [Fact] public async Task Should_log_before_and_after_request() { var context = new CommandContext(command, commandBus); await sut.HandleAsync(context, c => { context.Complete(true); return Task.CompletedTask; }); A.CallTo(log).Where(x => x.Method.Name == "Log" && x.GetArgument(0) == LogLevel.Debug) .MustHaveHappened(); A.CallTo(log).Where(x => x.Method.Name == "Log" && x.GetArgument(0) == LogLevel.Information) .MustHaveHappenedTwiceExactly(); } [Fact] public async Task Should_log_error_if_command_failed() { var context = new CommandContext(command, commandBus); await Assert.ThrowsAsync(async () => { await sut.HandleAsync(context, c => throw new InvalidOperationException()); }); A.CallTo(log).Where(x => x.Method.Name == "Log" && x.GetArgument(0) == LogLevel.Debug) .MustHaveHappened(); A.CallTo(log).Where(x => x.Method.Name == "Log" && x.GetArgument(0) == LogLevel.Information) .MustHaveHappened(); A.CallTo(log).Where(x => x.Method.Name == "Log" && x.GetArgument(0) == LogLevel.Error) .MustHaveHappened(); } [Fact] public async Task Should_log_if_command_is_not_handled() { var context = new CommandContext(command, commandBus); await sut.HandleAsync(context, c => Task.CompletedTask); A.CallTo(log).Where(x => x.Method.Name == "Log" && x.GetArgument(0) == LogLevel.Debug) .MustHaveHappened(); A.CallTo(log).Where(x => x.Method.Name == "Log" && x.GetArgument(0) == LogLevel.Information) .MustHaveHappenedTwiceExactly(); A.CallTo(log).Where(x => x.Method.Name == "Log" && x.GetArgument(0) == LogLevel.Critical) .MustHaveHappened(); } } }