Stage | CI | NuGet |
---|---|---|
Production | ||
Development |
Code that uses System.Net.Http.HttpClient
will attempt to actually call/hit that Http endpoint.
To prevent this from happening in a unit test, some simple helpers are provided in this code library.
- Use Dependency Injection to hijack your request (this library makes it supa dupa easy to do this)
- Works with GET/POST/etc.
- Can provide wildcards (i.e. I don't care about the Request endpoint or the request HTTP Method, etc)
- Can provide multiple endpoints and see handle what is returned based on the particular request.
- Can be used to test network errors during transmission. i.e. can test when the HttpClient throws an exception because of .. well ... ๐ฅ
Package Name: WorldDomination.HttpClient.Helpers
CLI: install-package WorldDomination.HttpClient.Helpers
There's plenty more examples about to wire up:
- A really simple example
- Multiple endpoints at once
- Wildcard endpoints
- Throwing exceptions and handling it
For all the samples, please check out the Wiki page: Helper Examples
Special Ackowledgements
A special and sincere thank you to David Fowler (@davidfowl) who explained how I should be unit testing the HttpClient
class and gave me the guidence to make this library. I was soooo on the wrong path - and he guided me back on track.
Thank you David! ๐ ๐ธ ๐พ
Finally, unit testing HttpClient
is now awesome and simple!