How to make a HttpClient request to TestServer instead of using TestServer.GetTestClient()

Sudhir Kumbhare 1 Reputation point
2021-03-12T09:29:14.017+00:00

I have built a unit test project in .NET Core 5.0 to test my API's. I have created a test server using HostBuilder. In my unit testing class I am calling an API using
HttpClient client;
client = TestServer.GetTestClient(); in all methods. It' working and no issues.

But in couple of unit test methods when request goes to BL(Business logic level) I need to make a call to TestServer to get the token then the actual problem starts. I can't make a call using Httpclient without using TestServer.GetTestClient() because if I do that it says the hosting server have actively refused to connect. And I can't make a call using TestServer.GetTestClient() because it will be a cyclic dependency on main project.

Is there a way to call TestServer using only Httpclient without using TestServer.GetTestClient()?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,158 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,119 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
328 questions
{count} votes