Creating mocks for your service calls is made easy with the use of the HttpClient and the FakeResponseHandler. The FakeResponseHandler inherits from the DelegatingHandler and will return a response based on the registered URI.
Here is a simple example of a proxy class that calls a REST service.
This is a simple example, but it works for the purposes of showing how to call a fake REST service. As you can see GetExample will be calling the GET method of the RestServiceExample controller in Web API. Notice the [Dependency] tag above the client property. I am injecting the HttpClient using Microsoft Unity. Now let’s assume we need to create a unit test for this method, but it relies on retrieving data from a service.
And here is a unit test that calls the GetExample method of the Proxy class. Notice the container.resolve in the test method. This will create an instance of the registered proxy class, and HttpClient, using dependency injection.
So, let’s take a look at how to connect the Unit Test to our fake REST service.
First we add the FakeResponseHandler class to our test project. This class is generic enough that it will be reused to fake any REST service. Only the registration details will change in our test initialization method.
Next I am going to should instantiate the fake response handler in the setup method of my unit test class. And add a Uri that I want the HttpClient to respond to, along with the desired status and content.
Then I am going to register the HttpClient and FakeResponseHandler with Microsoft Unity.
Now you should have an HttpClient that will respond to requests with fake data and response codes in your unit tests. Let’s take a look at the entire test class.
This great thing about this method is there is no need to create a mock class for HttpClient. Just by registering your HttpClient in the Unity configuration, you can control whether to call real REST services, or fakes.