Skip to content

Commit 4875a7b

Browse files
committed
Mocking thing
1 parent 367cb0f commit 4875a7b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/v107/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,28 @@ All the deprecated interfaces had only one implementation in RestSharp, so those
170170
What about mocking it, you might ask? The answer is: what would you do if you use a plain `HttpClient` instance? It doesn't implement any interface for the same reason - there's nothing to abstract, and there's only one implementation. We don't recommend mocking `RestClient` in your tests when you are testing against APIs that are controlled by you or people in your organisation. Test your clients against the real thing, as REST calls are I/O-bound. Mocking REST calls is like mocking database calls, and lead to a lot of issues in production even if all your tests pass against mocks.
171171

172172
As mentioned in [Recommended usage](#recommended-usage), we advise against using `RestClient` in the application code, and advocate wrapping it inside particular API client classes. Those classes would be under your control, and you are totally free to use interfaces there. If you absolutely must mock, you can mock your interfaces instead.
173+
174+
### Mocking
175+
176+
Mocking an infrastructure component like RestSharp (or HttpClient) is not the best idea. Even if you check that all the parameters are added correctly to the request, your "unit test" will only give you a false sense of safety that your code actually works. But, you have no guarantee that the remote server will accept your request, or if you can handle the actual response correctly.
177+
178+
The best way to test HTTP calls is to make some, using the actual service you call. However, you might still want to check if your API client forms requests in a certain way. You might also be sure about what the remote server responds to your calls with, so you can build a set of JSON (or XML) responses, so you can simulate remote calls.
179+
180+
It is perfectly doable without using interfaces. As RestSharp uses `HttpClient` internally, it certainly uses `HttpMessageHandler`. Features like delegating handlers allow you to intersect the request pipeline, inspect the request, and substitute the response. You can do it yourself, or use a library like [MockHttp](https://github.com/richardszalay/mockhttp). They have an example provided in the repository README, so we have changed it for RestClient here:
181+
182+
```csharp
183+
var mockHttp = new MockHttpMessageHandler();
184+
185+
// Setup a respond for the user api (including a wildcard in the URL)
186+
mockHttp.When("http://localhost/api/user/*")
187+
.Respond("application/json", "{'name' : 'Test McGee'}"); // Respond with JSON
188+
189+
// Instantiate the client normally, but replace the message handler
190+
var client = new RestClient(...) { ConfigureMessageHandler = _ => mockHttp };
191+
192+
var request = new RestRequest("http://localhost/api/user/1234");
193+
var response = await client.GetAsync(request);
194+
195+
// No network connection required
196+
Console.Write(response.Content); // {'name' : 'Test McGee'}
197+
```

0 commit comments

Comments
 (0)