aspnetcore/fundamentals/minimal-apis/test-min-api.md
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.
<a name="iit7"></a>
The following example shows how to unit test minimal route handlers that return xref:Microsoft.AspNetCore.Http.IResult using the xUnit testing framework. The external database is replaced with an in-memory database during testing, the implementation of the MockDb can be found in the sample code.
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 xref:Microsoft.AspNetCore.Http.HttpResults.NotFound%601 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 xref:Microsoft.AspNetCore.Http.HttpResults.Ok%601 class:
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoInMemoryTests.cs" id="snippet_1" highlight="18":::
In the previous examples, the result is cast to a concrete type because the endpoint under test can return multiple types (a xref:Microsoft.AspNetCore.Http.HttpResults.NotFound%601 or xref:Microsoft.AspNetCore.Http.HttpResults.Ok%601) result. However, if the endpoint returns a single xref:Microsoft.AspNetCore.Http.TypedResults type, then the result is automatically inferred to that type and no casting is required.
The following code uses the xref:Microsoft.AspNetCore.Http.TypedResults.Ok%2A class, and the value's type is a collection of Todo:
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/minimal-apis/samples/MinApiTestsSample/UnitTests/TodoInMemoryTests.cs" id="snippet_11" highlight="26":::