Use ASP.NET Core's TestServer in xUnit to Test Web API Endpoints: TestServer - Part 1

Опубликовано: 23 Октябрь 2024
на канале: Round The Code
4,237
66

Using ASP.NET Core's TestServer allows us to create an in-memory web server. This allows us to write xUnit tests, focusing on endpoints in a ASP.NET Core Web API application.

First, we will go ahead and set up our xUnit test project in Visual Studio 2019.

Afterwards, we will create a new instance of WebHostBuilder. Our instance of WebHostBuilder allows us to create a custom Startup class for our xUnit project.

We then pass this instance of WebHostBuilder into a new instance of TestServer. This is then setup to test our API endpoints.

In addition, we will have a look at ways of how we can use a SQL Server database in TestServer. This includes looking at Entity Framework's InMemory tool, allowing us to run an instance of EF without a database.

📖 Learn .NET and C# with our online courses 📖
https://www.roundthecode.com/dotnet-c...

► More Information: https://www.roundthecode.com/dotnet-t...

► ASP.NET Core's TestServer Playlist:    • ASP.NET Core's TestServer - Test Web ...  

#testserver #aspnetcore #xunit