forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionLoggerExtensionsTests.cs
More file actions
90 lines (75 loc) · 3.14 KB
/
Copy pathExceptionLoggerExtensionsTests.cs
File metadata and controls
90 lines (75 loc) · 3.14 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
// 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.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ExceptionHandling
{
public class ExceptionLoggerExtensionsTests
{
[Fact]
public async Task LogAsync_DelegatesToInterfaceLogAsync()
{
// Arrange
Task expectedTask = CreateCompletedTask();
Mock<IExceptionLogger> mock = new Mock<IExceptionLogger>(MockBehavior.Strict);
mock
.Setup(h => h.LogAsync(It.IsAny<ExceptionLoggerContext>(), It.IsAny<CancellationToken>()))
.Returns(expectedTask);
IExceptionLogger logger = mock.Object;
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
ExceptionContext expectedContext = CreateMinimalValidContext();
CancellationToken expectedCancellationToken = tokenSource.Token;
// Act
Task task = ExceptionLoggerExtensions.LogAsync(logger, expectedContext, expectedCancellationToken);
// Assert
Assert.Same(expectedTask, task);
await task;
mock.Verify(h => h.LogAsync(It.Is<ExceptionLoggerContext>(c => c.ExceptionContext == expectedContext),
expectedCancellationToken), Times.Once());
}
}
[Fact]
public void LogAsync_IfLoggerIsNull_Throws()
{
// Arrange
IExceptionLogger logger = null;
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgumentNull(() =>
ExceptionLoggerExtensions.LogAsync(logger, context, cancellationToken), "logger");
}
[Fact]
public void LogAsync_IfContextIsNull_Throws()
{
// Arrange
IExceptionLogger logger = CreateDummyLogger();
ExceptionContext context = null;
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgumentNull(() =>
ExceptionLoggerExtensions.LogAsync(logger, context, cancellationToken), "context");
}
private static CancellationTokenSource CreateCancellationTokenSource()
{
return new CancellationTokenSource();
}
private static Task CreateCompletedTask()
{
TaskCompletionSource<object> source = new TaskCompletionSource<object>();
source.SetResult(null);
return source.Task;
}
private static ExceptionContext CreateMinimalValidContext()
{
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static IExceptionLogger CreateDummyLogger()
{
return new Mock<IExceptionLogger>(MockBehavior.Strict).Object;
}
}
}