If we got to HttpClient class definition you will see that it does not implement any interface we can mock. Var result = await httpClient.GetStringAsync("sitemap.xml") Using(var httpClient = _httpClientFactory.CreateClient("sitemap")) Public DefaultController(IHttpClientFactory httpClientFactory) Readonly IHttpClientFactory _httpClientFactory Public class DefaultController : ControllerBase Since this application is ASP.NET Core application I will inject the service directly to controller using constructor. To make use of this injected service, we need to inject it in the class controller.
If you want to know more of how to easily retry and make your application more resilient to poor and unstable network connection check article Increase service resilience using Polly and retry pattern in ASP.NET Core
Now all client instances with name "sitemap" we use in our code will already have predefined base URL and retry policy configured by Polly. Public void Configure(IApplicationBuilder app, IWebHostEnvironment env) Use this method to configure the HTTP request pipeline. Services.AddHttpClient("sitemap", (provider, client) =>Ĭlient.BaseAddress = new This method gets called by the runtime. WaitAndRetryAsync(3, attempt => TimeSpan.FromSeconds(2)) OrResult(msg => msg.StatusCode = HttpStatusCode.Unauthorized) Public void ConfigureServices(IServiceCollection services) Use this method to add services to the container. This method gets called by the runtime. Public Startup(IConfiguration configuration) Since it is an interface it is easy to mock it for the class constructors, but when it comes to actual unit tests we need to mock HttpClient class instance.īefore we jump to an actual problem of writing a test for IHttpClientFactory and HttpClient which instance is create by IHttpClientFactory, let's see how the actual setup looks like in Startup.cs class file. Setting up IHttpClientFactory is quite easy in ASP.NET Core container setup in Startup.cs. NET Core we got IHttpClientFactory which allows us to have multiple configurations for HttpClient instances so that we do not need to repeat client setup. One of those classes is class. We use it so often to make web requests. Ideally when you need to mock something that is not and abstract class or interface you could always wrap it a class that implements interface which you could mock later.
There are still a lot of classes that we use daily in our code which we do not realize we cannot easily test until we get to writing unit tests for our existing code. If you want to know more about mocking System.IO classes you can checkout Mocking System.IO filesystem in unit tests in ASP.NET Core article. One of these classes come from namespace System.IO for file and folder operations, but luckily there are libraries that help you write testable code using System.IO classes. However, there are a lot of classes that re commonly used which are not refactored in. NET Core has done a great job by introducing interface for most of classes which makes them easy to write unit tests around them.