Skip to content

Repository files navigation

Verify.AspNetCore

DiscussionsBuild statusNuGet Status

Extends Verify to allow verification of AspNetCore bits.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Usage

Enable VerifyAspNetCore once at assembly load time:

[ModuleInitializer]publicstaticvoidInitialize()=>VerifyAspNetCore.Initialize();

snippet source | anchor

Controller

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;}}

snippet source | anchor

This test:

[Test]publicTaskTest(){varcontext=newControllerContext{HttpContext=newDefaultHttpContext()};varcontroller=newMyController{ControllerContext=context};varresult=controller.Method("inputValue");returnVerify(new{result,context});}

snippet source | anchor

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
}
}
}
}
}

snippet source | anchor

Middleware

Given the following middleware:

publicclassMyMiddleware(RequestDelegatenext){publicTaskInvoke(HttpContextcontext){context.Response.Headers["headerKey"]="headerValue";returnnext(context);}}

snippet source | anchor

This test:

[Test]publicasyncTaskTest(){varnextCalled=false;varmiddleware=newMyMiddleware(
_ =>{nextCalled=true;returnTask.CompletedTask;});varcontext=newDefaultHttpContext();awaitmiddleware.Invoke(context);awaitVerify(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.

[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";}

snippet source | anchor

ScrubAspTextResponse

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"));}

snippet source | anchor

Results in:

{
StatusCode: OK,
Headers: {
Content-Type: application/json
},
Value: {
key: replace
}
}

snippet source | anchor

Icon

Spider designed by marialuisa iborra from The Noun Project.

About

Extends Verify to allow verification of AspNetCore bits.

Resources

Code of conduct

Stars

7 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages