Перейти к файлу
GitHub Action 02a035cd48 Docs changes 2023-02-04 01:36:13 +00:00
.github Update merge-dependabot.yml 2022-08-17 14:40:34 +10:00
src Docs changes 2023-02-04 01:36:13 +00:00
.gitignore refs 2022-03-04 19:08:06 +11:00
code_of_conduct.md . 2021-10-01 15:29:48 +10:00
license.txt . 2021-10-01 15:29:48 +10:00
readme.md docs 2023-01-09 12:36:16 +11:00

readme.md

Verify.AspNetCore

Build status NuGet Status

Extends Verify to allow verification of AspNetCore bits.

NuGet package

https://nuget.org/packages/Verify.AspNetCore/

Usage

Enable VerifyAspNetCore once at assembly load time:

[ModuleInitializer]
public static void Initialize() =>
    VerifyAspNetCore.Enable();

snippet source | anchor

Controller

Given the following controller:

using Microsoft.AspNetCore.Mvc;

public class MyController :
    Controller
{
    public ActionResult<List<DataItem>> Method(string input)
    {
        var headers = HttpContext.Response.Headers;
        headers.Add("headerKey", "headerValue");
        headers.Add("receivedInput", input);

        var cookies = HttpContext.Response.Cookies;
        cookies.Append("cookieKey", "cookieValue");

        var items = new List<DataItem>
        {
            new("Value1"),
            new("Value2")
        };
        return new(items);
    }

    public class DataItem
    {
        public string Value { get; }

        public DataItem(string value) =>
            Value = value;
    }
}

snippet source | anchor

This test:

[Fact]
public Task Test()
{
    var context = new ControllerContext
    {
        HttpContext = new DefaultHttpContext()
    };
    var controller = new MyController
    {
        ControllerContext = context
    };

    var result = controller.Method("inputValue");
    return Verify(
        new
        {
            result,
            context
        });
}

snippet source | anchor

Will result in the following verified file:

{
  result: [
    {
      Value: Value1
    },
    {
      Value: Value2
    }
  ],
  context: {
    HttpContext: {
      Request: {},
      IsAbortedRequested: false,
      Response: {
        StatusCode: OK,
        Headers: {
          headerKey: headerValue,
          receivedInput: inputValue
        },
        Cookies: {
          cookieKey: cookieValue
        }
      }
    }
  }
}

snippet source | anchor

Middleware

Given the following middleware:

public class MyMiddleware
{
    RequestDelegate next;

    public MyMiddleware(RequestDelegate next) =>
        this.next = next;

    public async Task Invoke(HttpContext context)
    {
        context.Response.Headers.Add("headerKey", "headerValue");
        await next(context);
    }
}

snippet source | anchor

This test:

[Fact]
public async Task Test()
{
    var nextCalled = false;
    var middleware = new MyMiddleware(
        _ =>
        {
            nextCalled = true;
            return Task.CompletedTask;
        });

    var context = new DefaultHttpContext();
    await middleware.Invoke(context);

    await Verify(
        new
        {
            context.Response,
            nextCalled
        });
}

snippet source | anchor

Will result in the following verified file:

{
  Response: {
    StatusCode: OK,
    Headers: {
      headerKey: headerValue
    }
  },
  nextCalled: true
}

snippet source | anchor

Testing a web app with specific controller scenarios

UseSpecificControllers extends IMvcBuilder to allow integration testing of a web app using a specific controller scenario.

[Fact]
public async Task ControllerIntegrationTest()
{
    var builder = WebApplication.CreateBuilder();

    var controllers = builder.Services.AddControllers();
    // custom extension
    controllers.UseSpecificControllers(typeof(FooController));

    await using var app = builder.Build();
    app.MapControllers();

    await app.StartAsync();

    using var client = new HttpClient();
    var result = client.GetStringAsync($"{app.Urls.First()}/Foo");

    await Verify(result);
}

[ApiController]
[Route("[controller]")]
public class FooController :
    ControllerBase
{
    [HttpGet]
    public string Get() =>
        "Foo";
}

snippet source | anchor

Icon

Spider designed by marialuisa iborra from The Noun Project.