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()?
Mario ScheerenPosted Mar 12, 2021, 5:27 PM
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using System;
using System.IO;
using System.Net.Http;
using Xunit;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;