2.2 KiB
title | author | description | ms.author | ms.date | monikerRange | uid |
---|---|---|---|---|---|---|
Test Minimal API apps | rick-anderson | Unit and integration tests in Minimal API apps | wpickett | 05/31/2024 | >= aspnetcore-7.0 | fundamentals/minimal-apis/test-min-api |
Unit and integration tests in Minimal API apps
By Fiyaz Bin Hasan, and Rick Anderson
The sample code on GitHub provides an example of unit and integration tests on a Minimal API app.
IResult implementation types
Public xref:Microsoft.AspNetCore.Http.IResult implementation types in the xref:Microsoft.AspNetCore.Http.HttpResults?displayProperty=fullName namespace can be used to unit test minimal route handlers when using named methods instead of lambdas.
The following code uses the NotFound<TValue>
class:
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoInMemoryTests.cs" id="snippet_" highlight="8":::
The following code uses the Ok<TValue>
class:
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoInMemoryTests.cs" id="snippet_1" highlight="18":::
Additional Resources
- Basic authentication tests is not a .NET repository but was written by a member of the .NET team. It provides examples of basic authentication testing.
- View or download sample code
- xref:fundamentals/minimal-apis/security
- Use port tunneling Visual Studio to debug web APIs
- xref:mvc/controllers/testing
- xref:test/razor-pages-tests