Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve VerifyNoOutstandingExpectation message outpu #78

Open
aligneddev opened this issue Apr 21, 2020 · 6 comments
Open

Improve VerifyNoOutstandingExpectation message outpu #78

aligneddev opened this issue Apr 21, 2020 · 6 comments

Comments

@aligneddev
Copy link

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.

@hwoodiwiss
Copy link

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?

@hwoodiwiss
Copy link

@richardszalay Would you accept community contributions towards this, as I would be interested in working on this.

@hwoodiwiss
Copy link

hwoodiwiss commented Feb 13, 2022

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.

@hwoodiwiss
Copy link

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?

@aligneddev
Copy link
Author

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

@hwoodiwiss
Copy link

hwoodiwiss commented Feb 21, 2022

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'}");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants