Testing custom exception middleware

Viewed 30

The exception middleware is working, but I would like to write some unit tests for it.

This is what I have:

public async Task InvokeAsync(HttpContext httpContext)
{
    try
    {
        await _next(httpContext);
    }
    catch (Exception ex)
    {
        await HandleExceptionAsync(httpContext, ex);
    }
}

private static async Task HandleExceptionAsync(HttpContext httpContext, Exception exception)
{
    httpContext.Response.ContentType = "application/json";

    httpContext.Response.StatusCode = exception switch
    {
        KeyNotFoundException => StatusCodes.Status404NotFound,
        ItemNotFoundException => StatusCodes.Status404NotFound,
        MyItemNotFoundException => StatusCodes.Status404NotFound,
        ItemDetailNotFoundException => StatusCodes.Status404NotFound,
        ItemIsExpiredException => StatusCodes.Status422UnprocessableEntity,
        _ => StatusCodes.Status500InternalServerError
    };

    await httpContext.Response.WriteAsync(JsonSerializer.Serialize(exception.Message));
}

This is what I'm trying to do in my tests :

// Arrange
var middleware = new ExceptionMiddleware((innerHttpContext) =>
{
    throw new KeyNotFoundException("Test", "Test");
});

var context = new DefaultHttpContext();
context.Response.Body = new MemoryStream();

// Act
await middleware.InvokeAsync(context);

context.Response.Body.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(context.Response.Body);
var streamText = reader.ReadToEnd();
//var objResponse = JsonConvert.DeserializeObject<CustomErrorResponse>(streamText);

var sut = new MyController(_unitOfWorkStub.Object, _myServiceStub.Object);

var result = await sut.MyMethodAsync(It.IsAny<string>(), It.IsAny<MyRequest>()) as ObjectResult;

It seems to work but do I need to tests this separately or can I test this in my 'Controller' test?

I want to test that my controller returns the correct status code and message.

0 Answers
Related