add tests
This commit is contained in:
82
ChatBot.Tests/TestUtilities/TestBase.cs
Normal file
82
ChatBot.Tests/TestUtilities/TestBase.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
|
||||
namespace ChatBot.Tests.TestUtilities;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for integration tests with common setup
|
||||
/// </summary>
|
||||
public abstract class TestBase : IDisposable
|
||||
{
|
||||
protected IServiceProvider ServiceProvider { get; private set; } = null!;
|
||||
protected Mock<ILogger> LoggerMock { get; private set; } = null!;
|
||||
|
||||
protected virtual void SetupServices()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
// Add logging
|
||||
LoggerMock = new Mock<ILogger>();
|
||||
services.AddSingleton(LoggerMock.Object);
|
||||
services.AddLogging(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
|
||||
|
||||
// Add other common services
|
||||
ConfigureServices(services);
|
||||
|
||||
ServiceProvider = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
protected abstract void ConfigureServices(IServiceCollection services);
|
||||
|
||||
protected virtual void Cleanup()
|
||||
{
|
||||
if (ServiceProvider is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Cleanup();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base class for unit tests with common assertions
|
||||
/// </summary>
|
||||
public abstract class UnitTestBase
|
||||
{
|
||||
protected static void AssertLogMessage(Mock<ILogger> loggerMock, LogLevel level, string message)
|
||||
{
|
||||
loggerMock.Verify(
|
||||
x =>
|
||||
x.Log(
|
||||
level,
|
||||
It.IsAny<EventId>(),
|
||||
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains(message)),
|
||||
It.IsAny<Exception>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||
),
|
||||
Times.AtLeastOnce
|
||||
);
|
||||
}
|
||||
|
||||
protected static void AssertLogError(Mock<ILogger> loggerMock, string message)
|
||||
{
|
||||
AssertLogMessage(loggerMock, LogLevel.Error, message);
|
||||
}
|
||||
|
||||
protected static void AssertLogWarning(Mock<ILogger> loggerMock, string message)
|
||||
{
|
||||
AssertLogMessage(loggerMock, LogLevel.Warning, message);
|
||||
}
|
||||
|
||||
protected static void AssertLogInformation(Mock<ILogger> loggerMock, string message)
|
||||
{
|
||||
AssertLogMessage(loggerMock, LogLevel.Information, message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user