0

i want to unit test a repository that uses the the HttpClientFactory.i asked the question how do i unit test a repo that calls IHttpClientFactory with Nunit. Helpful enough someone suggested i check a similar question which was asked at How to mock the new HttpClientFactory in .NET Core 2.1 using Moq. Following the suggested solution,i stumbled another problem,a method in the most voted answer to the question is not working. the line of code in solution is

_handlerFunc = (request, cancellationToken) => Task.FromResult(request.CreateResponse(HttpStatusCode.OK));

The method request.CreateResponse seems that it does not exist for the request object.Am using asp.netcore 3.1

David Tembo
  • 79
  • 3
  • 10

1 Answers1

0

CreateResponse is an extension method that uses the request to create a corresponding response.

You could forego that extension method and create the HttpResponseMessage manually.

_handlerFunc = (request, cancellationToken) => 
    Task.FromResult(new HttpResponseMessage { StatusCode = HttpStatusCode.OK });`
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thanks i just did that.now am having problems with ``new HttpConfiguration()`` it seems its under System.Web.Http Namespace which was supported in asp.net core2.1 and am using asp.netcore 3.1.please help am really stuck with these tests..nothing seems to be working – David Tembo May 13 '20 at 09:48
  • @DavidTembo remove `HttpConfiguration` if you are creating the response manually. It wont be needed. It was there because the extension method needed it. – Nkosi May 13 '20 at 09:53
  • 1
    So how can i arrange the tests without the extension method. without the extension method i get the error `` An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set. at System.Net.Http.HttpClient.PrepareRequestMessage`` – David Tembo May 13 '20 at 10:21