mockhttp
mockhttp copied to clipboard
Improve VerifyNoOutstandingExpectation message outpu
Currently, when I have a mismatched .Expect, and call mockHttp.VerifyNoOutstandingExpectation(), I get this message "System.InvalidOperationException: There are 1 unfulfilled expectations".
It'd be really nice for it to list the calls that were made to the mockHttp and the actual value.
If we have this call (from the readme)
mockHttp.Expect("/tokens/refresh")
.WithFormData("refresh_token", "refresh_token")
.Respond("application/json", "{'access_token' : 'new_token', 'refresh_token' : 'new_refresh'}");
and FormData is something else the the output could say. There are 1 unfulfilled expectations. $"/tokens/refresh was called with {formData}"
similar for withContent, etc.
Thanks for making this Nuget package. It's been very helpful for my unit testing.
I have been considering how this could also be further improved by the CallerArgumentExpressionAttribute, as this would give latitude to output the full expression of the matcher(s) that failed wouldn't it?
@richardszalay Would you accept community contributions towards this, as I would be interested in working on this.
I've been considering the design of this. I was thinking of extending the definition of IMockedRequestMatcher to include a Description function, which can populated for each matcher type, and used to generate a descriptive list of unmatched matchers on failure.
I was also thinking for the CustomMatcher that this would be a good use case for the new CallerArgumentExpression attribute, as the likely use case will be users of the custom matchers will by writing a lambda directly in MockedRequest.With which we'd then be able to display to them directly.
In the current implementation in #92, the exception message looks like:
There are 1 unfulfilled expectations:
Expected A request that matches:
A URL matching: /test
AND HTTP Method matching: POST
AND Headers matching:
Header: HeaderValue
AND Form Data matching:
FormField = FormValue
AND Matching at least one of:
(Content Partially matching: TestContent
OR Content Partially matching: SomeOtherContent)
For multiple unfulfilled requests, they're also separated by an AND so:
There are 2 unfulfilled expectations:
Expected A request that matches:
A URL matching: /test
AND HTTP Method matching: POST
AND Headers matching:
Header: HeaderValue
AND Form Data matching:
FormField = FormValue
AND A request that matches:
A URL matching: /test
AND HTTP Method matching: HEAD
AND Headers matching:
Header: HeaderValue
AND Form Data matching:
FormField = FormValue
This should be a good start to the feature and imo should allow developers to pretty easily see what might be missing. Does anyone else have thoughts on this?
This looks like a good step forward. I like where this is going.
If match doesn't specify headers or form data, etc, could you skip showing it?
For example: I'm calling /api/people/1234 and mockHttp.Expect("/api/people/1234") then only show
There are 1 unfulfilled expectations: Expected A request that matches: A URL matching: /api/people/1234 AND HTTP Method matching: GET
That's how it works at the moment, it will only display information for configured matchers on an expected request.
For example, a configuration that would yield my first example if unmatched would look like:
mockHandler
.Expect(HttpMethod.Post, "/test")
.WithHeaders(new Dictionary<string, string>
{
{ "Header", "HeaderValue" }
})
.WithFormData($"FormField=FormValue")
.WithAny(new PartialContentMatcher("TestContent"), new PartialContentMatcher("SomeOtherContent"))
.Respond("application/json", "{'status' : 'First'}");