You cannot unit test it like that. As you mentioned: HttpClient is a dependency, and therefore it must be introduced.
Personally, I would create my own IHttpClient interface implemented by HttpClientWrapper , which wraps around System.Net.HttpClient . IHttpClient will be passed as a dependency on your contructor object.
As follows, HttpClientWrapper cannot be tested per unit. However, I would write a couple of integration tests to make sure that the shell is well written.
Edit
IHttpClient does not have to be a "valid" interface for HttpClient . It should only be an interface that suits your needs. It can have as many or as few methods as possible.
Portray this: HttpClient allows you to do many things. But in your project, you only call the GetAsync(uri).Result , nothing more.
Given this scenario, you should write the following interface and implementation:
interface IHttpClient { HttpResponseMessage Get(string uri); } class HttpClientWrapper : IHttpClient { private readonly HttpClient _client; public HttpClientWrapper(HttpClient client) { _client = client; } public HttpResponseMessage Get(string uri) { return _client.GetAsync(new Uri(uri)).Result; } }
So, as I said earlier, the interface should suit your needs. You do not need to wrap the WHOLE HttpClient class.
Obviously, you then moq your object as follows:
var clientMock = new Mock<IHttpClient>();
And to create the actual object:
var client = new HttpClientWrapper(new HttpClient()); var myobj = new MyClass(client );
Edit2
HE! And don't forget that IHttpClient should also extend the IDisposable interface, very important!