Skip to content

Commit fa08ee1

Browse files
committed
Attempt to start exercising the various paths of OnException.
Not working. ExceptionContext is the big roadblock. Can't seem to mock that guy.
1 parent 244b811 commit fa08ee1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/Test.Unit.Infrastructure.WebApi/Exceptions/NoteBookExceptionFilterTester.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
namespace Test.Unit.Infrastructure.WebApi.Exceptions
22
{
3+
using System;
34
using CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Exceptions;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc.Filters;
47
using Microsoft.Extensions.Logging;
58
using NSubstitute;
69
using NUnit.Framework;
@@ -21,5 +24,34 @@ public void CanConstructNoteBookExceptionFilter()
2124
Assert.That(subjectUnderTest, Is.Not.Null);
2225
Assert.That(subjectUnderTest, Is.TypeOf(typeof(NoteBookExceptionFilter)));
2326
}
27+
28+
public void OnExceptionHandlesUnauthorized()
29+
{
30+
// ARRANGE
31+
var expectedException = Substitute.For<UnauthorizedAccessException>();
32+
var expectedHttpContext = Substitute.For<HttpContext>();
33+
var expectedHttpResponse = Substitute.For<HttpResponse>();
34+
// var thing = Substitute.For<ExceptionContextCatchBlock>();
35+
var context = Substitute.For<ExceptionContext>(expectedException, null);
36+
37+
context.Exception.Returns(expectedException);
38+
expectedException.GetType().Returns(typeof(UnauthorizedAccessException));
39+
context.HttpContext.Returns(expectedHttpContext);
40+
expectedHttpContext.Response.Returns(expectedHttpResponse);
41+
42+
var logger = Substitute.For<ILogger<NoteBookExceptionFilter>>();
43+
var subjectUnderTest = new NoteBookExceptionFilter(logger);
44+
45+
// ACT
46+
subjectUnderTest.OnException(context);
47+
48+
// ASSERT
49+
var temp1 = context.Received(2).Exception;
50+
expectedException.Received(1).GetType();
51+
logger.Received(1).LogError(0, expectedException, "Unauthorized Access");
52+
var temp2 = context.Received(1).HttpContext;
53+
var temp3 = expectedHttpContext.Received(1).Response;
54+
expectedHttpResponse.ReceivedWithAnyArgs(1).WriteAsync(Arg.Any<string>());
55+
}
2456
}
2557
}

0 commit comments

Comments
 (0)