Awesome
<img src="/src/icon.png" height="30px"> 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:
<!-- snippet: Enable --><a id='snippet-Enable'></a>
[ModuleInitializer]
public static void Initialize() =>
VerifyAspNetCore.Initialize();
<sup><a href='/src/Tests/ModuleInitializer.cs#L3-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-Enable' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Controller
Given the following controller:
<!-- snippet: MyController.cs --><a id='snippet-MyController.cs'></a>
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;
}
}
<sup><a href='/src/Tests/Snippets/MyController.cs#L1-L27' title='Snippet source file'>snippet source</a> | <a href='#snippet-MyController.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->This test:
<!-- snippet: MyControllerTest --><a id='snippet-MyControllerTest'></a>
[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
});
}
<sup><a href='/src/Tests/Snippets/MyControllerTests.cs#L5-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-MyControllerTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Will result in the following verified file:
<!-- snippet: MyControllerTests.Test.verified.txt --><a id='snippet-MyControllerTests.Test.verified.txt'></a>
{
result: [
{
Value: Value1
},
{
Value: Value2
}
],
context: {
HttpContext: {
Request: {},
IsAbortedRequested: false,
Response: {
StatusCode: OK,
Headers: {
headerKey: headerValue,
receivedInput: inputValue
},
Cookies: {
cookieKey: cookieValue
}
}
}
}
}
<sup><a href='/src/Tests/Snippets/MyControllerTests.Test.verified.txt#L1-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-MyControllerTests.Test.verified.txt' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Middleware
Given the following middleware:
<!-- snippet: MyMiddleware.cs --><a id='snippet-MyMiddleware.cs'></a>
public class MyMiddleware(RequestDelegate next)
{
public Task Invoke(HttpContext context)
{
context.Response.Headers["headerKey"] = "headerValue";
return next(context);
}
}
<sup><a href='/src/Tests/Snippets/MyMiddleware.cs#L1-L8' title='Snippet source file'>snippet source</a> | <a href='#snippet-MyMiddleware.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->This test:
<!-- snippet: MyMiddlewareTest --><a id='snippet-MyMiddlewareTest'></a>
[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
});
}
<sup><a href='/src/Tests/Snippets/MyMiddlewareTests.cs#L3-L25' title='Snippet source file'>snippet source</a> | <a href='#snippet-MyMiddlewareTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Will result in the following verified file:
<!-- snippet: MyMiddlewareTests.Test.verified.txt --><a id='snippet-MyMiddlewareTests.Test.verified.txt'></a>
{
Response: {
StatusCode: OK,
Headers: {
headerKey: headerValue
}
},
nextCalled: true
}
<sup><a href='/src/Tests/Snippets/MyMiddlewareTests.Test.verified.txt#L1-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-MyMiddlewareTests.Test.verified.txt' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Testing a web app with specific controller scenarios
UseSpecificControllers
extends IMvcBuilder
to allow integration testing of a web app using a specific controller scenario.
<a id='snippet-TestController'></a>
[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";
}
<sup><a href='/src/Tests/Tests.cs#L78-L110' title='Snippet source file'>snippet source</a> | <a href='#snippet-TestController' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Icon
Spider designed by marialuisa iborra from The Noun Project.