Extends Verify to allow verification of AspNetCore bits.
See Milestones for release notes.
Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.
Enable VerifyAspNetCore once at assembly load time:
[ModuleInitializer]publicstaticvoidInitialize()=>VerifyAspNetCore.Initialize();Given the following controller:
publicclassMyController:Controller{publicActionResult<List<DataItem>>Method(stringinput){varheaders=HttpContext.Response.Headers;headers["headerKey"]="headerValue";headers["receivedInput"]=input;varcookies=HttpContext.Response.Cookies;cookies.Append("cookieKey","cookieValue");varitems=newList<DataItem>{new("Value1"),new("Value2")};returnnew(items);}publicclassDataItem(stringvalue){publicstringValue{get;}=value;}}This test:
[Test]publicTaskTest(){varcontext=newControllerContext{HttpContext=newDefaultHttpContext()};varcontroller=newMyController{ControllerContext=context};varresult=controller.Method("inputValue");returnVerify(new{result,context});}Will result in the following verified file:
{
result: [
{
Value: Value1
},
{
Value: Value2
}
],
context: {
HttpContext: {
Request: {},
Response: {
StatusCode: OK,
Headers: {
headerKey: headerValue,
receivedInput: inputValue
},
Cookies: {
cookieKey: cookieValue
}
}
}
}
}Given the following middleware:
publicclassMyMiddleware(RequestDelegatenext){publicTaskInvoke(HttpContextcontext){context.Response.Headers["headerKey"]="headerValue";returnnext(context);}}This test:
[Test]publicasyncTaskTest(){varnextCalled=false;varmiddleware=newMyMiddleware(
_ =>{nextCalled=true;returnTask.CompletedTask;});varcontext=newDefaultHttpContext();awaitmiddleware.Invoke(context);awaitVerify(new{context.Response,nextCalled});}Will result in the following verified file:
{
Response: {
StatusCode: OK,
Headers: {
headerKey: headerValue
}
},
nextCalled: true
}UseSpecificControllers extends IMvcBuilder to allow integration testing of a web app using a specific controller scenario.
[Test]publicasyncTaskControllerIntegrationTest(){varbuilder=WebApplication.CreateBuilder();varcontrollers=builder.Services.AddControllers();// custom extensioncontrollers.UseSpecificControllers(typeof(FooController));awaitusingvarapp=builder.Build();app.MapControllers();awaitapp.StartAsync();usingvarclient=newHttpClient();varresult=client.GetStringAsync($"{app.Urls.First()}/Foo");awaitVerify(result);}[ApiController][Route("[controller]")]publicclassFooController:ControllerBase{[HttpGet]publicstringGet()=>"Foo";}The ScrubAspTextResponse feature allows modification or scrubbing of the content of an HTTP response before verification. This is useful for scenarios where the response contains dynamic or sensitive data that needs to be replaced or removed.
[Test]publicTaskScrubAspTextResponse(){varcontext=newDefaultHttpContext();varbuffer="{\"key\":\"value\"}"u8;varresponse=context.Response;response.Body=newMemoryStream(buffer.ToArray());response.ContentType="application/json";returnVerify(response).ScrubAspTextResponse(_ =>_.Replace("value","replace"));}Results in:
{
StatusCode: OK,
Headers: {
Content-Type: application/json
},
Value: {
key: replace
}
}Spider designed by marialuisa iborra from The Noun Project.

