-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestLoggingExamples.cs
More file actions
69 lines (57 loc) · 2.3 KB
/
TestLoggingExamples.cs
File metadata and controls
69 lines (57 loc) · 2.3 KB
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
66
67
68
69
// License:
// Apache License Version 2.0, January 2004
// Authors:
// Aleksander Kovač
using com.github.akovac35.Logging.Testing;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using Shared.Mocks;
using System;
namespace TestApp
{
[TestFixture]
public class TestLoggingExamples
{
[OneTimeSetUp]
public void OneTimeSetUp()
{
customOnWrite = writeContext => {
Console.WriteLine(writeContext);
};
customOnBeginScope = scopeContext => {
Console.WriteLine(scopeContext);
};
serviceCollection = new ServiceCollection();
// Register TestLogger using extension method (uses TryAdd),
// always register it as the first service for reliable registration
serviceCollection.AddTestLogger(onWrite: customOnWrite, onBeginScope: customOnBeginScope);
serviceCollection.AddTransient(typeof(BusinessLogicMock<>));
}
[SetUp]
public void SetUp()
{
}
private IServiceCollection serviceCollection;
private Action<WriteContext> customOnWrite;
private Action<ScopeContext> customOnBeginScope;
[Test]
public void Test_WithoutLoggers_Works()
{
// Demonstrate that logging doesn't need to be configured for normal operation of the business logic if it is properly designed
var test = new BusinessLogicMock<object>();
Assert.AreEqual(test.SecondLevel(123), 123);
Assert.AreNotEqual(test.SecondLevel(123), 0);
}
[Test]
public void Test_WithLoggingToTestConsole_Works()
{
// The service provider should be defined on per-test level or logger writes will accumulate and may result in OOM - clean them with testSink.Clear()
var serviceProvider = serviceCollection.BuildServiceProvider();
var blm = serviceProvider.GetRequiredService<BusinessLogicMock<object>>();
blm.FirstLevel();
var testSink = serviceProvider.GetRequiredService<ITestSink>();
Assert.IsTrue(testSink.Writes.Count > 0);
Assert.IsTrue(testSink.Scopes.Count > 0);
}
}
}