66e200cdf6 | ||
---|---|---|
.github | ||
src | ||
.gitattributes | ||
.gitignore | ||
code_of_conduct.md | ||
license.txt | ||
readme.md |
readme.md
Verify.AspNetCore
Extends Verify to allow verification of AspNetCore bits.
See Milestones for release notes.
NuGet package
https://nuget.org/packages/Verify.AspNetCore/
Usage
Enable VerifyAspNetCore once at assembly load time:
[ModuleInitializer]
public static void Initialize() =>
VerifyAspNetCore.Initialize();
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["headerKey"] = "headerValue";
headers["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(string value)
{
public string Value { get; } = value;
}
}
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
});
}
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
}
}
}
}
}
Middleware
Given the following middleware:
public class MyMiddleware(RequestDelegate next)
{
public Task Invoke(HttpContext context)
{
context.Response.Headers["headerKey"] = "headerValue";
return next(context);
}
}
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
});
}
Will result in the following verified file:
{
Response: {
StatusCode: OK,
Headers: {
headerKey: headerValue
}
},
nextCalled: true
}
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";
}
Icon
Spider designed by marialuisa iborra from The Noun Project.