2 Replies - 252 Views - Last Post: 16 January 2019 - 09:01 AM

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 250
  • Joined: 25-May 16

Unit Test HttpClient/HttpClientHandler

Posted 16 January 2019 - 08:06 AM

Hi everyone, I am trying to do a few unit tests and am at a halt for right now.

I have a class called HttpSvc which implements one of our interfaces called IHttpSvc.

The code goes a little like this

public class HttpSvc: IHttpSvc
{
 private readonly HttpClient _serviceClient;

 // constructor which will get Base Address from the appSettings.json file
 public HttpSvc(IConfiguration config)
 {
  _serviceClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
  _serviceClient.BaseAddress = new Uri(config.GetSection("AppSettings:Urls")["ServiceClient"]);
 }

 public HttpClient GetServiceClient()
 {
  return _serviceClient;
 }

 public IList<HttpClient> GetHttpClients()
 {
  return new List<HttpClient>()
  {
   _serviceClient;
  }
 }
}



That's the class I'm trying to Mock or at least have some way of faking it so that it doesn't make any real calls to another web service that I have. I read that HttpClient does not inherit from any interface, so I am stuck on that part and on what I should do from there.

Any tips?

Is This A Good Question/Topic? 0
  • +

Replies To: Unit Test HttpClient/HttpClientHandler

#2 andrewsw   User is online

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,046
  • Joined: 12-December 12

Re: Unit Test HttpClient/HttpClientHandler

Posted 16 January 2019 - 09:00 AM

I noticed that there were a number of results returned on a search...

How to mock HttpClient in your .NET / C# unit tests
Was This Post Helpful? 1
  • +
  • -

#3 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 250
  • Joined: 25-May 16

Re: Unit Test HttpClient/HttpClientHandler

Posted 16 January 2019 - 09:01 AM

I just finished reading that article. That appears to be the fix that I have been looking for. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1