This is the document https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1 we can see that IHttpClientFactory is registered :
services.AddHttpClient();
and the model class that consume it as:
public BasicUsageModel(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task OnGet()
{
...
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(request);
...
}
but how about we don't register it, and we just create a new instance of it as:
public async Task OnGet()
{
...
var client = new HttpClient();
var response = await client.SendAsync(request);
...
}
so that the BasicUsageModel's constructor doesn't need to take any argument, and when do unit testing , I don't need to use Moq to mock it, isn't that ever better?
and for the first case, how can I mock the IHttpClientFactory to test OnGet() without actually send a request?