-
Notifications
You must be signed in to change notification settings - Fork 5
/
LoggingTestWithInjectedFactory.cs
65 lines (54 loc) · 2.24 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
54
55
56
57
58
59
60
61
62
63
64
65
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
namespace SampleWebApplicationNLog.IntegrationTests
{
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.GetTestLoggerSink().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.TryGetTestLoggerSink(out var testLoggerSink)) testLoggerSink.Clear();
}
[Fact]
public async Task ShouldLogHelloWorld()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/");
// Assert
var log = Assert.Single(_factory.GetTestLoggerSink().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.GetTestLoggerSink().LogEntries);
var scope = Assert.Single(log.Scopes);
// Assert the scope rendered by a default formatter
Assert.Equal("I'm in the GET scope", scope.Message);
}
[Fact]
public async Task ShouldBeginScope()
{
// Arrange
// Act
await _factory.CreateDefaultClient().GetAsync("/");
// Assert
var scope = Assert.Single(_factory.GetTestLoggerSink().Scopes);
// Assert the scope rendered by a default formatter
Assert.Equal("I'm in the GET scope", scope.Message);
}
}
}