-
Notifications
You must be signed in to change notification settings - Fork 5
/
LoggingTestWithInjectedFactory.cs
53 lines (45 loc) · 1.95 KB
/
LoggingTestWithInjectedFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Serilog.Events;
using Xunit;
namespace SampleWebApplicationSerilogAlternate.IntegrationTests
{
[Collection("Serilog Test Collection")]
public class LoggingTestWithInjectedFactory : IClassFixture<CustomWebApplicationFactory<Startup>>
{
private readonly CustomWebApplicationFactory<Startup> _factory;
public LoggingTestWithInjectedFactory(CustomWebApplicationFactory<Startup> factory)
{
_factory = factory;
// In this case, the factory will be reused for all tests, so the sink will be shared as well.
// We can clear the sink before each test execution, as xUnit will not run this tests in parallel.
_factory.GetSerilogTestLoggerSink().Clear();
// When running on 2.x, the server is not initialized until it is explicitly started or the first client is created.
// So we need to use:
// if (_factory.TryGetSerilogTestLoggerSink(out var testLoggerSink)) testLoggerSink.Clear();
}
[Fact]
public async Task ShouldLogHelloWorld()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
// Assert the message rendered by a default formatter
Assert.Equal("Hello \"World\"!", log.Message);
}
[Fact]
public async Task ShouldUseScope()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/");
// Assert
var log = Assert.Single(_factory.GetSerilogTestLoggerSink().LogEntries);
var scope = Assert.Single(log.Scopes);
var scopeValue = Assert.IsType<ScalarValue>(scope).Value;
Assert.Equal("I'm in the GET scope", scopeValue);
}
}
}