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 "Member x does not exist" failure message #852

Closed
jessfdm-codes opened this issue Jun 27, 2019 · 5 comments
Closed

Improve "Member x does not exist" failure message #852

jessfdm-codes opened this issue Jun 27, 2019 · 5 comments
Assignees

Comments

@jessfdm-codes
Copy link
Contributor

Currently an ArgumentException is thrown when a .Protected().Verify(methodName, times, arguments) call is made when arguments doesn't match the arguments in methodName's signature which just states System.ArgumentException : Member Class.Method does not exist..

For example, if mocking a HttpMessageHandler you would use the following code to check if SendAsync was called with any (correct type) arguments:

mockHttpMessageHandler.Protected().Verify(
           "SendAsync",
           Times.Exactly(1),
           ItExpr.IsAny<HttpRequestMessage>(),
           ItExpr.IsAny<CancellationToken>()
);

If I was to accidentally use the wrong generic type for one of the arguments (for example string instead of CancellationToken) my code would instead look like this:

mockHttpMessageHandler.Protected().Verify(
           "SendAsync",
           Times.Exactly(1),
           ItExpr.IsAny<HttpRequestMessage>(),
           ItExpr.IsAny<string>()
);

and, then the test is run would give the following error:

System.ArgumentException : Member HttpMessageHandler.SendAsync does not exist.

This gives the impression that there is no such method as SendAsync at all in the class, whereas it's only the case that there isn't a SendAsync signature which takes (HttpRequestMessage, string). Might it be worth updating this error message to include the erroneous signature i.e. something like:

System.ArgumentException: Member HttpMessageHandler.SendAsync(HttpRequestMessage, string) does not exist.

to make it easier to debug why tests are failing?

@stakx
Copy link
Contributor

stakx commented Jun 27, 2019

@thomasfdm, not sure how often people run into this problem, but having good diagnostic messages can't hurt. Would you like to submit a PR?

@jessfdm-codes
Copy link
Contributor Author

Happy to give it a go, but no definite ETA on it 😄

@jessfdm-codes
Copy link
Contributor Author

I'll try and get it done today

@stakx
Copy link
Contributor

stakx commented Jul 13, 2019

@thomasfdm - One thing to keep in mind (IIRC): It might be wrong to assume that the method parameter list has to match exactly with the provided arguments. (For example, on some .Protected().... method overloads, you might request exactParameterMatch: false.) So it might be more correct to generate an error message saying, "No protected method X was found whose parameters are compatible with those provided: (T1, T2, T3, ...)", instead of, "No protected method X(T1, T2, T3, ...) found." The latter might be just as misleading (or worse) as what we have now.

@jessfdm-codes
Copy link
Contributor Author

jessfdm-codes commented Jul 13, 2019

Good catch, I've changed my error message accordingly:
No Protected Method Class.Method found whose parameters are compatible with provided types (T1, T2, ... Tx)

I've now got the method to generate it, just need to replace the calls from ThrowIfMissingMember to this new method. I'll leave the previous message in place for properties as it is at the moment.

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

No branches or pull requests

2 participants