How to figure out the the URL when 'No matching mock handler' exception occurs?
Closed this issue · 3 comments
I'm getting a No matching mock handler
in my unit test which is most likely caused by a typo in my expectation:
var mockHttp = new MockHttpMessageHandler();
mockHttp.Expect(HttpMethod.Get, "...the url...").Respond("application/json", "...the response...");
but I can't figure out what I typed wrong. Is there any way to get the actual URL?
I added a fallback like so:
var fiddlerProxy = new WebProxy("http://localhost:8888");
var handler = new HttpClientHandler { Proxy = fiddlerProxy, UseProxy = true };
mockHttp.Fallback.Respond(new HttpClient(handler));
which allowed me to see the request in Fiddler and figure out my typo but still, I'm curious if there's a simpler way to achieve this that wouldn't involve using a third party tool.
If you have access to the error No matching mock handler
then you should have access to the HttpResponseMessage
, in which case you can just access the url via response.RequestMessage.RequestUri
.
You could even configure fallback to include that URI in the response message:
// eg. No matching mock handler found for "GET http://localhost/abc"
mockHttp.Fallback.Response(req => new HttpResponseMessage(System.Net.HttpStatusCode.NotFound)
{
ReasonPhrase = $"No matching mock handler found for \"{req.Method.ToString().ToUpperInvariant()} {req.RequestUri.AbsoluteUri}\""
});
Excellent suggestion! In fact, I suggest this should be the built-in behavior.