Testing with Flurl
When developing against REST APIs without dedicated client libraries, I very much like to use Flurl.Http, because it makes it pretty easy and intuitive to test your code, also under several error conditions.
Assume the following production method:
1 | public Task<Dto> GetTenantLicenseAsync(long tenantId, CancellationToken ct = default) => |
(Note that for the purposes of this example, the code leaves out authentication, but you can work with this just as well.)
First, note how nicely you build the requests. This already is courtesy of Flurl. But that in itself would not much distinguish it from the likes of RestSharp and similar libraries. The really interesting part comes when you want to test your code. Let’s start with a test for the happy path:
1 | [ ] |
As you see, it’s really quite simple to setup responses and check whether the right calls were made.
Now let’s look at simulating errors:
1 | [ ] |
In our example, the production code doesn’t do anything to handle the exception, so we expect it to be thrown, but you could just as well test your sophisticated error-handling behavior this way.
However, there are certain error conditions you cannot easily simulate even with Flurl:
- no network connectivity
- DNS resolution issues
- generally, most low-level socket related problems
If it’s enough for you to test a general “network not available” handler, then you can help yourself by setting up your client with an invalid base-address. This will trigger a SocketError.HostNotFound
, so if your code doesn’t distinguish between different socket-level error-conditions, you got a sufficient testing strategy with this.