vendredi 3 janvier 2020

How to unit test a .NET middleware that uses Response.OnStarting

So, I created a .net core middleware that adds a header to a response. I am not finding a way to unit test this middleware, as it uses the OnStarting callback and I can't imagine how to mock or force its execution.

Here is a sample of the middleware:

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
    context.Response.OnStarting(() =>
    {
        context.Response.Headers.Add("Name", "Value");

        return Task.CompletedTask;
    });

    await next(context);
}

As the middleware interface expects a concrete instance of HttpContext, I can't imagine a way to mock it using FakeItEasy, for example.

Aucun commentaire:

Enregistrer un commentaire