-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathExamples.cs
54 lines (41 loc) · 1.44 KB
/
Examples.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
// Copyright (c) Martin Costello, 2018. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace MartinCostello.Logging.XUnit;
public class Examples(ITestOutputHelper outputHelper)
{
[Fact]
public void Calculator_Sums_Two_Equal_Integers()
{
// Arrange using conversion to a logger
var calculator = new Calculator(outputHelper.ToLogger<Calculator>());
// Act
int actual = calculator.Sum(2, 2);
// Assert
actual.ShouldBe(4);
}
[Fact]
public void Calculator_Sums_Two_Different_Integers()
{
// Arrange using the logging provider
var services = new ServiceCollection()
.AddLogging((builder) => builder.AddXUnit(outputHelper))
.AddSingleton<Calculator>();
IServiceProvider provider = services.BuildServiceProvider();
var calculator = provider.GetRequiredService<Calculator>();
// Act
int actual = calculator.Sum(1, 2);
// Assert
actual.ShouldBe(3);
}
private sealed class Calculator(ILogger<Calculator> logger)
{
public int Sum(int x, int y)
{
int sum = x + y;
logger.LogInformation("The sum of {X} and {Y} is {Sum}.", x, y, sum);
return sum;
}
}
}