Skip to content

How to make mockery detect typos in foo.On("MyMethox")? #765

Answered by LandonTClipp
guettli asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, usage of the .EXPECT() builder pattern. This returns a struct with methods that are (mostly) identical to your interface. So one of its methods would be, in your example, .MethodWhichWantsAString("foobar"). This will automatically call out the appropriate expectation in testify. This method is a bit more type safe as if you can't misspell the method name, and if you miss an argument, you'll get compiler errors.

It's not 100% fool proof because the argument types in the method are interface{} because we need a way to specify mock.Anything. A way to get around that is something like mock.EXPECT().MethodWhichWantsAString(mock.Anything).RunAndReturn(func(s string){ return "foo" }). This …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@guettli
Comment options

Answer selected by LandonTClipp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants