mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.5 KiB
89 lines
2.5 KiB
// ==========================================================================
|
|
// LogExceptionHandlerTests.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Xunit;
|
|
|
|
namespace Squidex.Infrastructure.CQRS.Commands
|
|
{
|
|
public class LogExceptionHandlerTests
|
|
{
|
|
private readonly MyLogger logger = new MyLogger();
|
|
private readonly LogExceptionHandler sut;
|
|
|
|
private sealed class MyLogger : ILogger<LogExceptionHandler>
|
|
{
|
|
public int LogCount { get; private set; }
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatterr)
|
|
{
|
|
LogCount++;
|
|
}
|
|
|
|
public bool IsEnabled(LogLevel logLevel)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public IDisposable BeginScope<TState>(TState state)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private sealed class MyCommand : ICommand
|
|
{
|
|
}
|
|
|
|
public LogExceptionHandlerTests()
|
|
{
|
|
sut = new LogExceptionHandler(logger);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_do_nothing_if_command_is_succeeded()
|
|
{
|
|
var context = new CommandContext(new MyCommand());
|
|
|
|
context.Succeed();
|
|
|
|
var isHandled = await sut.HandleAsync(context);
|
|
|
|
Assert.False(isHandled);
|
|
Assert.Equal(0, logger.LogCount);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_log_if_command_failed()
|
|
{
|
|
var context = new CommandContext(new MyCommand());
|
|
|
|
context.Fail(new InvalidOperationException());
|
|
|
|
var isHandled = await sut.HandleAsync(context);
|
|
|
|
Assert.False(isHandled);
|
|
Assert.Equal(1, logger.LogCount);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_log_if_command_is_not_handled()
|
|
{
|
|
var context = new CommandContext(new MyCommand());
|
|
|
|
context.Fail(new InvalidOperationException());
|
|
|
|
var isHandled = await sut.HandleAsync(context);
|
|
|
|
Assert.False(isHandled);
|
|
Assert.Equal(1, logger.LogCount);
|
|
}
|
|
}
|
|
}
|