I have webapi where it needs to call some other endpoint and get data.
My current code as follows
//http client implementation
public interface IHttpClientFactory { HttpClient Create(); } public class HttpClientFactory : IHttpClientFactory { private readonly ApplicationSettings _applicationSettings; HttpClient _httpClient; public HttpClientFactory(IOptions<ApplicationSettings> settings) { _applicationSettings = settings.Value; } public HttpClient Create() { if (_httpClient != null) return _httpClient; var client = new HttpClient() { BaseAddress = new Uri($"{_applicationSettings.BaseUrl}") }; _httpClient = client; return _httpClient; } } public interface IGetItemsQuery { Task<IEnumerable<T>> Execute<T>(string url); } public class GetItemQuery: IGetItemsQuery { private readonly IHttpClientFactory _httpClientFactory; public GetPhotosQuery(IHttpClientFactory httpClientFactory) { _httpClientFactory = httpClientFactory; } public async Task<IEnumerable<T>> Execute<T>(string url) { using (var response = await _httpClientFactory.Create().GetAsync($"{url}").ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); var resp = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var items = JArray.Parse(resp); return items.ToObject<T[]>(); } } In my controller part
private readonly IGetItemsQuery _getItemsQuery; public HomeController(IGetItemsQuery getItemsQuery) { _getItemsQuery = getItemsQuery; } appsettings "ApplicationSettings": { "BaseUrl": "http://someendpoint.com/" }
Startup
services.Configure<ApplicationSettings>(Configuration.GetSection("ApplicationSettings")); services.AddScoped<IGetItemsQuery, GetPhotosQuery>(); services.AddScoped<IHttpClientFactory, HttpClientFactory>(); I want to try something like below in my test
[Fact] public void Test_Index() { // Arrange var itemsQuery = new Mock<IGetItemsQuery>(); var controller = new HomeController(itemsQuery.Object); // Act var result = controller.Index(); // Assert var viewResult = Assert.IsType<ViewResult>(result); Assert.Null(viewResult.ViewName); } This is creating mock IGetItemsQuery but this isn't mocking the actual IHttpClientFactory.
Is there a way to do this