Skip to content

Commit

Permalink
test: use Serilog.Sinks.InMemory (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu authored Feb 13, 2023
1 parent b890c89 commit c1b9c5b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ public static class Constants
{
public const string AppName = "test-web";
public const string IntegrationEventIdHeaderName = "X-IntegrationEvent-Id";
}

public static class LogTemplates
{
public const string HandledIntegratonEvent = "Handled integration event {@event}.";
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
using Cnblogs.Architecture.TestIntegrationEvents;
using static Cnblogs.Architecture.IntegrationTestProject.Constants;

namespace Cnblogs.Architecture.IntegrationTestProject.EventHandlers;

public class TestIntegrationEventHandler : IIntegrationEventHandler<TestIntegrationEvent>
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger _logger;

public TestIntegrationEventHandler(IHttpContextAccessor httpContextAccessor, ILogger<TestIntegrationEventHandler> logger)
public TestIntegrationEventHandler(ILogger<TestIntegrationEventHandler> logger)
{
_httpContextAccessor = httpContextAccessor;
_logger = logger;
}

public Task Handle(TestIntegrationEvent notification, CancellationToken cancellationToken)
{
var context = _httpContextAccessor.HttpContext;
context?.Response.OnStarting(() =>
{
context.Response.Headers.Add(Constants.IntegrationEventIdHeaderName, notification.Id.ToString());
return Task.CompletedTask;
});

_logger.LogInformation("Handled integration event {event}.", notification);
_logger.LogInformation(LogTemplates.HandledIntegratonEvent, notification);

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddCnblogsApiVersioning();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();

var app = builder.Build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Cnblogs.Serilog.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand All @@ -21,6 +22,12 @@
<ProjectReference Include="..\Cnblogs.Architecture.IntegrationTestProject\Cnblogs.Architecture.IntegrationTestProject.csproj" />
<ProjectReference Include="..\Cnblogs.Architecture.TestShared\Cnblogs.Architecture.TestShared.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog.Sinks.InMemory" Version="0.11.0" />
<PackageReference Include="Serilog.Sinks.InMemory.Assertions" Version="0.11.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Sinks.InMemory;
using Serilog.Sinks.InMemory.Assertions;
using Xunit.Abstractions;
using static Cnblogs.Architecture.IntegrationTestProject.Constants;

namespace Cnblogs.Architecture.IntegrationTests;

Expand All @@ -24,15 +29,15 @@ public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync()
{
// Arrange
var builder = WebApplication.CreateBuilder();
builder.Logging.AddSerilog(logger => logger.WriteTo.InMemory().WriteTo.Console());
builder.Services
.AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly)
.AddHttpContextAccessor();
.AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly);
builder.WebHost.UseTestServer();
var app = builder.Build();
app.Subscribe<TestIntegrationEvent>();
await app.StartAsync();
var client = app.GetTestClient();
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!");
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, $"Hello World! {Guid.NewGuid()}");

// Act
var subscriptions = await client.GetFromJsonAsync<Subscription[]>("/dapr/subscribe");
Expand All @@ -42,7 +47,8 @@ public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync()

// Assert
response.Should().BeSuccessful();
response.Headers.Should().ContainKey(Constants.IntegrationEventIdHeaderName)
.WhoseValue.First().Should().Be(@event.Id.ToString());
InMemorySink.Instance
.Should().HaveMessage(LogTemplates.HandledIntegratonEvent).Appearing().Once()
.WithProperty("event").HavingADestructuredObject().WithProperty("Id").WithValue(@event.Id);
}
}

0 comments on commit c1b9c5b

Please sign in to comment.