-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
mock: print caller info on unexpected method call #1448
base: master
Are you sure you want to change the base?
mock: print caller info on unexpected method call #1448
Conversation
212be46
to
d291745
Compare
d291745
to
c3e1218
Compare
@CodiumAI-Agent /review |
PR Analysis
PR Feedback
How to use
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that printing callerInfo would be a neat thing to do. LGTM.
Please provide a test running on Here is a template: https://go.dev/play/p/yobgjBYsyWz |
Here is an example which shows a test that would benefit from the extra information. In this test it doesn't tell you which of the calls to DoSomething failed. With the fix in this MR the location of the call that wasn't matched is printed out so the developer immediately knows which call is not being called with the correct input/what is missing in the mocks. |
Reproducer, simplified: https://go.dev/play/p/LqwLNJpzo9e The information is in the stacktrace, but having it directly in the message would be helpful. |
mock/mock.go
Outdated
@@ -491,11 +491,12 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen | |||
m.mutex.Unlock() | |||
|
|||
if closestCall != nil { | |||
m.fail("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\nDiff: %s", | |||
m.fail("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\nDiff: %s\n\nat: %s", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix formatting to match the style of the other message in the other branch of the if.
m.fail("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\nDiff: %s\n\nat: %s", | |
m.fail("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\n\tat: %s\nDiff: %s", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've fixed it up so that it uses \n\t
but I feel like the caller info should go last as it does for both of the other scenarios. I'm not too fussed either way though if you're keen for diff to be last
callString(methodName, arguments, true), | ||
callString(methodName, closestCall.Arguments, true), | ||
diffArguments(closestCall.Arguments, arguments), | ||
strings.TrimSpace(mismatch), | ||
assert.CallerInfo(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the caller info nearer to the function call, above the diff.
Summary
Print caller info when an unexpected method call is made on a mock and a "closest call" can be found
Motivation
Both other call failures print this info so this change makes it consistent across the three failures.
The caller info is useful information when using the mock to develop tests.
Related issues
None that I could find
Example test that benefits from this change
https://go.dev/play/p/hZulK4Fc_1_I