Five Useful Tips When Using ASP.NET Core's TestServer in xUnit: TestServer - Part 2

Published: 24 November 2020
on channel: Round The Code
1,154
32

We look at five useful tips when using ASP.NET Core's TestServer in xUnit.

Tip #1: TestServer's DbContext Instance 0:51 - By default, when you run Entity Framework in a web application, the lifetime of the DbContext is scoped. This is fine if you are using a SQL Server database. But when using Entity Framework's in-memory database, we cannot store the data. So we have a look at how we can change the service lifetime when using TestServer.

Tip #2: Post Request on Test Server 2:22 - It's relatively straight forward to perform a GET request when using TestServer. But, there is slightly more work when doing a POST request. We go ahead and do a POST request with our ASP.NET Core Web API to create an entity. We then make sure that it has been created by doing a test against the read endpoint.

Tip #3: Add Authorization Header into a TestServer Request 9:20 - Your API endpoints may require authorisation. Maybe, you are using OAuth 2 security and require to add a Bearer token to your request. We show you how to add an Authorization header to both a GET and POST request when using TestServer

Tip #4: Use The Same Instance of TestServer in All Tests 10:14 - With multiple tests potentially running at the same time, you could have multiple instances of TestServer. This could have a drain on memory. We show you how to run all your tests on one instance of TestServer.

Tip #5: Add appsettings.json to xUnit Project 13:28 - We show you how to add a custom appsettings.json file to your TestServer configuration when using it in xUnit. This is beneficial if you are specifying a connection string that is different from your ASP.NET Core Web API application.

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

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

► Part 1:    • Use ASP.NET Core's TestServer in xUni...  
► ASP.NET Core's TestServer Playlist:    • ASP.NET Core's TestServer - Test Web ...  

► Chapters
0:00 Welcome
0:51 Tip #1: TestServer's DbContext Instance
2:22 Tip #2: Post request on TestServer
9:20 Tip #3: Add Authorization header into a TestServer request
10:14 Tip #4: Use the same instance of TestServer in all tests
13:28 Tip #5: Add appsettings.json to xUnit project
16:35 Conclusion

#testserver #aspnetcore #xunit


Watch video Five Useful Tips When Using ASP.NET Core's TestServer in xUnit: TestServer - Part 2 online without registration, duration hours minute second in high quality. This video was added by user Round The Code 24 November 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,154 once and liked it 32 people.