Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Include stacktrace when no matching expected call#65

Closed
RJPercival wants to merge 2 commits intogolang:masterfrom
RJPercival:stacktrace
Closed

Include stacktrace when no matching expected call#65
RJPercival wants to merge 2 commits intogolang:masterfrom
RJPercival:stacktrace

Conversation

@RJPercival
Copy link
Copy Markdown

This helps with tracking down the offending call.

@RJPercival
Copy link
Copy Markdown
Author

This may be obsolete, now that #97 has been merged. It depends whether a full stack trace is considered preferable. Feel free to close this if not.

@balshetzer
Copy link
Copy Markdown
Collaborator

I do think a stack trace could be useful but I want to make sure that the output is more helpful than confusing. I need some sample outputs for that but haven't gotten around to making some on my own. If you want to produce those for me then we could get there faster :)

Rob Percival added 2 commits August 21, 2017 17:08
This helps with tracking down the offending call.
This helps with tracking down the offending expectation.
@RJPercival
Copy link
Copy Markdown
Author

I've just modified the NotMatcher test to fail in order to get sample output.

No matching expected call:

FAIL | NotMatcher (0.00s)
     |   controller.go:132: Unexpected call to *mock_gomock.MockMatcher.Matches([4]) because: 
     |     Expected call at /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/matchers_test.go:61 doesn't match the argument at index 0.
     |     Got: 4
     |     Want: is equal to 3
     |     
     |     goroutine 36 [running]:
     |     runtime/debug.Stack(0xc4200de2d0, 0x555ca0, 0xc4200e4070)
     |       /usr/lib/google-golang/src/runtime/debug/stack.go:24 +0xa7
     |     github.com/golang/mock/gomock.(*Controller).Call(0xc42000b620, 0x555ca0, 0xc4200e4070, 0x57b9e6, 0x7, 0xc4200e40e0, 0x1, 0x1, 0x0, 0x0, ...)
     |       /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/controller.go:132 +0x397
     |     github.com/golang/mock/gomock/mock_matcher.(*MockMatcher).Matches(0xc4200e4070, 0x543480, 0x597f10, 0x1)
     |       /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/mock_matcher/mock_matcher.go:36 +0xac
     |     github.com/golang/mock/gomock.notMatcher.Matches(0x7f216d6913e8, 0xc4200e4070, 0x543480, 0x597f10, 0xc4200e40d0)
     |       /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/matchers.go:82 +0x45
     |     github.com/golang/mock/gomock.(*notMatcher).Matches(0xc4200e4080, 0x543480, 0x597f10, 0x1)
     |       <autogenerated>:1 +0x59
     |     github.com/golang/mock/gomock_test.TestNotMatcher(0xc42009fe00)
     |       /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/matchers_test.go:62 +0x29e
     |     testing.tRunner(0xc42009fe00, 0x585878)
     |       /usr/lib/google-golang/src/testing/testing.go:747 +0xd0
     |     created by testing.(*T).Run
     |       /usr/lib/google-golang/src/testing/testing.go:790 +0x2de
     |   controller.go:177: missing call(s) to *mock_gomock.MockMatcher.Matches(is equal to 3) /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/matchers_test.go:61
     |   controller.go:184: aborting test due to missing call(s)
FAIL | github.com/golang/mock/gomock  0.014s

Invalid call expectation:

FAIL | NotMatcher (0.00s)
     |   call.go:107: wrong type of argument 0 to Return for *mock_gomock.MockMatcher.Matches: string is not assignable to bool
     |     goroutine 36 [running]:
     |     runtime/debug.Stack(0x543c00, 0x63a6c0, 0x541e40)
     |       /usr/lib/google-golang/src/runtime/debug/stack.go:24 +0xa7
     |     github.com/golang/mock/gomock.(*Call).Return(0xc4200e0270, 0xc4200e40d0, 0x1, 0x1, 0xc4200e4070)
     |       /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/call.go:108 +0x2c4
     |     github.com/golang/mock/gomock_test.TestNotMatcher(0xc42009fe00)
     |       /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/matchers_test.go:61 +0x268
     |     testing.tRunner(0xc42009fe00, 0x585878)
     |       /usr/lib/google-golang/src/testing/testing.go:747 +0xd0
     |     created by testing.(*T).Run
     |       /usr/lib/google-golang/src/testing/testing.go:790 +0x2de
     |   controller.go:177: missing call(s) to *mock_gomock.MockMatcher.Matches(is equal to 4) /usr/local/google/home/robpercival/go/src/github.com/golang/mock/gomock/matchers_test.go:61
     |   controller.go:184: aborting test due to missing call(s)
FAIL | github.com/golang/mock/gomock  0.016s

The call stack does end up containing a few calls internal to gomock before it gets to the user's test code, but I don't think that's too confusing. With some extra code, it should be possible to strip some of these internal calls (the top two at least), but I'm not sure if that would be more or less confusing.

@msabramo
Copy link
Copy Markdown
Contributor

msabramo commented Apr 10, 2018

See #171. I did a bit of filtering of the stack so it starts with the code that made the call (not gomock) and it goes up the stack until it hits a filename with _test.go in it, so we don't have to look at the test runner's stack.

Sample output:

--- FAIL: TestNotMatcher (0.00s)
	controller.go:172: Unexpected call to *mock_gomock.MockMatcher.Matches([4]) at /Users/abramowi/go/src/github.com/golang/mock/gomock/mock_matcher/mock_matcher.go:37 because:
		Expected call at /Users/abramowi/go/src/github.com/golang/mock/gomock/matchers_test.go:61 doesn't match the argument at index 0.
		Got: 4
		Want: is equal to 3

		github.com/golang/mock/gomock/mock_matcher.(*MockMatcher).Matches
			/Users/abramowi/go/src/github.com/golang/mock/gomock/mock_matcher/mock_matcher.go:37
		github.com/golang/mock/gomock.notMatcher.Matches
			/Users/abramowi/go/src/github.com/golang/mock/gomock/matchers.go:82
		github.com/golang/mock/gomock.(*notMatcher).Matches
			<autogenerated>:1
		github.com/golang/mock/gomock_test.TestNotMatcher
			/Users/abramowi/go/src/github.com/golang/mock/gomock/matchers_test.go:62
	asm_amd64.s:573: missing call(s) to *mock_gomock.MockMatcher.Matches(is equal to 3) /Users/abramowi/go/src/github.com/golang/mock/gomock/matchers_test.go:61
	asm_amd64.s:573: aborting test due to missing call(s)

@poy
Copy link
Copy Markdown
Collaborator

poy commented Nov 30, 2018

@RJPercival Thanks for the PR. This no longer merges and has been open for a while. I'm going to close it, but feel free to reopen it.

@poy poy closed this Nov 30, 2018
@RJPercival
Copy link
Copy Markdown
Author

@poy: It was open for a while because no one reviewed it, but I'm happy for #171 to supercede this since it provides more concise stack traces.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants