Share via

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

Anonymous
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()?

Developer technologies | .NET | .NET Runtime
Developer technologies | Visual Studio | Testing
Developer technologies | ASP.NET Core | Other

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.