Skip to content

Commit c1b9c5b

Browse files
authored
test: use Serilog.Sinks.InMemory (#22)
1 parent b890c89 commit c1b9c5b

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

test/Cnblogs.Architecture.IntegrationTestProject/Constants.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ public static class Constants
44
{
55
public const string AppName = "test-web";
66
public const string IntegrationEventIdHeaderName = "X-IntegrationEvent-Id";
7-
}
7+
8+
public static class LogTemplates
9+
{
10+
public const string HandledIntegratonEvent = "Handled integration event {@event}.";
11+
}
12+
}
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
22
using Cnblogs.Architecture.TestIntegrationEvents;
3+
using static Cnblogs.Architecture.IntegrationTestProject.Constants;
34

45
namespace Cnblogs.Architecture.IntegrationTestProject.EventHandlers;
56

67
public class TestIntegrationEventHandler : IIntegrationEventHandler<TestIntegrationEvent>
78
{
8-
private readonly IHttpContextAccessor _httpContextAccessor;
99
private readonly ILogger _logger;
1010

11-
public TestIntegrationEventHandler(IHttpContextAccessor httpContextAccessor, ILogger<TestIntegrationEventHandler> logger)
11+
public TestIntegrationEventHandler(ILogger<TestIntegrationEventHandler> logger)
1212
{
13-
_httpContextAccessor = httpContextAccessor;
1413
_logger = logger;
1514
}
1615

1716
public Task Handle(TestIntegrationEvent notification, CancellationToken cancellationToken)
1817
{
19-
var context = _httpContextAccessor.HttpContext;
20-
context?.Response.OnStarting(() =>
21-
{
22-
context.Response.Headers.Add(Constants.IntegrationEventIdHeaderName, notification.Id.ToString());
23-
return Task.CompletedTask;
24-
});
25-
26-
_logger.LogInformation("Handled integration event {event}.", notification);
18+
_logger.LogInformation(LogTemplates.HandledIntegratonEvent, notification);
2719

2820
return Task.CompletedTask;
2921
}
30-
}
22+
}

test/Cnblogs.Architecture.IntegrationTestProject/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
1818
builder.Services.AddCnblogsApiVersioning();
1919
builder.Services.AddSwaggerGen();
20-
builder.Services.AddHttpContextAccessor();
2120

2221
var app = builder.Build();
2322

test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
3+
<PackageReference Include="Cnblogs.Serilog.Extensions" Version="1.1.0" />
34
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.2" />
45
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
56
<PackageReference Include="xunit" Version="2.4.2" />
@@ -21,6 +22,12 @@
2122
<ProjectReference Include="..\Cnblogs.Architecture.IntegrationTestProject\Cnblogs.Architecture.IntegrationTestProject.csproj" />
2223
<ProjectReference Include="..\Cnblogs.Architecture.TestShared\Cnblogs.Architecture.TestShared.csproj" />
2324
</ItemGroup>
25+
26+
<ItemGroup>
27+
<PackageReference Include="Serilog.Sinks.InMemory" Version="0.11.0" />
28+
<PackageReference Include="Serilog.Sinks.InMemory.Assertions" Version="0.11.0" />
29+
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
30+
</ItemGroup>
2431

2532
<ItemGroup>
2633
<Folder Include="Properties\" />

test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
using Microsoft.AspNetCore.Builder;
77
using Microsoft.AspNetCore.TestHost;
88
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
10+
using Serilog;
11+
using Serilog.Sinks.InMemory;
12+
using Serilog.Sinks.InMemory.Assertions;
913
using Xunit.Abstractions;
14+
using static Cnblogs.Architecture.IntegrationTestProject.Constants;
1015

1116
namespace Cnblogs.Architecture.IntegrationTests;
1217

@@ -24,15 +29,15 @@ public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync()
2429
{
2530
// Arrange
2631
var builder = WebApplication.CreateBuilder();
32+
builder.Logging.AddSerilog(logger => logger.WriteTo.InMemory().WriteTo.Console());
2733
builder.Services
28-
.AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly)
29-
.AddHttpContextAccessor();
34+
.AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly);
3035
builder.WebHost.UseTestServer();
3136
var app = builder.Build();
3237
app.Subscribe<TestIntegrationEvent>();
3338
await app.StartAsync();
3439
var client = app.GetTestClient();
35-
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!");
40+
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, $"Hello World! {Guid.NewGuid()}");
3641

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

4348
// Assert
4449
response.Should().BeSuccessful();
45-
response.Headers.Should().ContainKey(Constants.IntegrationEventIdHeaderName)
46-
.WhoseValue.First().Should().Be(@event.Id.ToString());
50+
InMemorySink.Instance
51+
.Should().HaveMessage(LogTemplates.HandledIntegratonEvent).Appearing().Once()
52+
.WithProperty("event").HavingADestructuredObject().WithProperty("Id").WithValue(@event.Id);
4753
}
4854
}

0 commit comments

Comments
 (0)