forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeExceptionLoggerTests.cs
More file actions
146 lines (121 loc) · 5.44 KB
/
Copy pathCompositeExceptionLoggerTests.cs
File metadata and controls
146 lines (121 loc) · 5.44 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ExceptionHandling
{
public class CompositeExceptionLoggerTests
{
[Fact]
public void Constructor_IfLoggersIsNull_Throws()
{
// Arrange
IEnumerable<IExceptionLogger> loggers = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => CreateProductUnderTest(loggers), "loggers");
}
[Fact]
public void Loggers_ContainsSpecifiedInstances()
{
// Arrange
IExceptionLogger expectedLogger = CreateDummyLogger();
List<IExceptionLogger> expectedLoggers = new List<IExceptionLogger> { expectedLogger };
CompositeExceptionLogger product = CreateProductUnderTest(expectedLoggers);
// Act
IEnumerable<IExceptionLogger> loggers = product.Loggers;
// Assert
Assert.NotNull(loggers);
IExceptionLogger logger = Assert.Single(loggers);
Assert.Same(expectedLogger, logger);
}
[Fact]
public void Constructor_CapturesLoggersContents()
{
// Arrange
IExceptionLogger expectedLogger = CreateDummyLogger();
List<IExceptionLogger> expectedLoggers = new List<IExceptionLogger> { expectedLogger };
// Act
CompositeExceptionLogger product = CreateProductUnderTest(expectedLoggers);
// Assert
expectedLoggers.Clear();
IEnumerable<IExceptionLogger> loggers = product.Loggers;
Assert.NotNull(loggers);
IExceptionLogger logger = Assert.Single(loggers);
Assert.Same(expectedLogger, logger);
}
[Fact]
public void ConstructorWithParams_SetsLoggers()
{
// Arrange
IExceptionLogger expectedLogger1 = CreateDummyLogger();
IExceptionLogger expectedLogger2 = CreateDummyLogger();
// Act
CompositeExceptionLogger product = new CompositeExceptionLogger(expectedLogger1, expectedLogger2);
// Assert
IEnumerable<IExceptionLogger> loggers = product.Loggers;
Assert.NotNull(loggers);
Assert.Equal(2, loggers.Count());
Assert.Same(expectedLogger1, loggers.ElementAt(0));
Assert.Same(expectedLogger2, loggers.ElementAt(1));
}
[Fact]
public async Task LogAsync_DelegatesToLoggers()
{
// Arrange
Mock<IExceptionLogger> exceptionLogger1Mock = new Mock<IExceptionLogger>(MockBehavior.Strict);
Mock<IExceptionLogger> exceptionLogger2Mock = new Mock<IExceptionLogger>(MockBehavior.Strict);
exceptionLogger1Mock
.Setup(l => l.LogAsync(It.IsAny<ExceptionLoggerContext>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(0));
exceptionLogger2Mock
.Setup(l => l.LogAsync(It.IsAny<ExceptionLoggerContext>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(0));
IEnumerable<IExceptionLogger> loggers = new IExceptionLogger[]
{
exceptionLogger1Mock.Object,
exceptionLogger2Mock.Object
};
IExceptionLogger product = CreateProductUnderTest(loggers);
ExceptionLoggerContext expectedContext = CreateMinimalValidContext();
CancellationToken expectedCancellationToken = CreateCancellationToken();
// Act
await product.LogAsync(expectedContext, expectedCancellationToken);
// Assert
exceptionLogger1Mock.Verify(l => l.LogAsync(expectedContext, expectedCancellationToken), Times.Once());
exceptionLogger2Mock.Verify(l => l.LogAsync(expectedContext, expectedCancellationToken), Times.Once());
}
[Fact]
public void LogAsync_IfLoggerIsNull_Throws()
{
// Arrange
IEnumerable<IExceptionLogger> loggers = new IExceptionLogger[] { null };
IExceptionLogger product = CreateProductUnderTest(loggers);
ExceptionLoggerContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CreateCancellationToken();
// Act & Assert
Assert.Throws<InvalidOperationException>(() => product.LogAsync(context, cancellationToken),
"The IExceptionLogger instance must not be null.");
}
private static CancellationToken CreateCancellationToken()
{
CancellationTokenSource source = new CancellationTokenSource();
return source.Token;
}
private static ExceptionLoggerContext CreateMinimalValidContext()
{
return new ExceptionLoggerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
}
private static IExceptionLogger CreateDummyLogger()
{
return new Mock<IExceptionLogger>(MockBehavior.Strict).Object;
}
private static CompositeExceptionLogger CreateProductUnderTest(IEnumerable<IExceptionLogger> loggers)
{
return new CompositeExceptionLogger(loggers);
}
}
}