Add argument captors #262
Description
Description
I think it would be nice to build an ArgumentCaptor
interface and argumentCaptor
struct that extends Matcher
through composition, but store the value of the arguments before the "match" step. Those argument values could then be retrieved for later assertions (very close to how Mockito's ArgumentCaptors work in Java) https://static.javadoc.io/org.mockito/mockito-core/2.6.9/org/mockito/ArgumentCaptor.html
Use Case
I recently ran into a situation where I wanted to write a test that asserted something about the contents of an argument (a slice of struct pointers) passed to a mocked method. Using matchers as they currently exist (i.e. eqMatcher
), I couldn't find a good way to do that without already knowing the memory addresses of the pointers in the slice.
Activity
[-]Add argument captors to gomock[/-][+]Add argument captors[/+]poy commentedon Feb 12, 2019
Have you looked at the
gomock.Do()
(docs)? It seems like this would get you pretty close.tylersammann commentedon Feb 12, 2019
Thanks for the reply @poy! Yes, I started writing my tests with
gomock.Do()
and it was working ok. However, the methods I was mocking had long complicated signatures, so the anonymous funcs I needed to pass toDo()
were very long. TheArgumentCaptor
s get the same end result with a lot less code in those situations.tylersammann commentedon Feb 12, 2019
I also tried writing a decorator function for passing into
Do()
that looked something like this.It involves writing less code, but its downside was the need to grab the arguments based on their order in the signature with an index.
poy commentedon Feb 24, 2019
@tylersammann
Sorry for the slow response. @balshetzer and I are discussing how we would like to approach adding matchers. I'll let you know what decide!
tylersammann commentedon Jun 5, 2019
@poy @balshetzer Any news here?
shivasuri commentedon Aug 28, 2019
+1
Infiniverse commentedon Dec 1, 2019
+1
hgl commentedon Jan 5, 2020
@poy @balshetzer
I'm struggling to test the following scenario with
Do()
:Say my function
foo()
calls two mocked method.F1()
and.F2()
, I want to test the argument passed to.F1()
is the same as the one passed to.F2()
This obviously won't work. Any good way to test this right now or the new matchers can help with that?
tylersammann commentedon Feb 11, 2020
@hgl I think there might be a way to do something similar to this with
.Do()
, but it would involve storing the arguments used in bothF1
andF2
and comparing them afterwards. FWIW, here is how I think I'd do with with my argument captor implementation from this PR #263