ArgumentCaptor struct and tests#263
Conversation
- added new ArguemntCaptor interface - added argumentCaptor implementation that composes Matcher - added constructors for ArgumentCaptor - included unit tests for ArgumentCaptor methods
- added sample service and dao interface - added unit test that demonstrates `AnyCaptor` usage
Argument Captor initial release
|
What is the advantage of this over just using |
|
Also, I would like to say, I like the idea of gomock being able to generate "spies" in some way, but I want to ensure the API is solid. |
|
@poy gotcha. I wrote a little bit about using In terms of API change size, I think I'm only adding one 37 line file to the API itself. https://github.com/golang/mock/blob/bdfd0e24d230faaafd3c9d815aab7c1266ce9685/gomock/argument_captor.go The rest of the additions are testing or sample code related. (plus one I am happy to discuss changes to the API though if you can see places to improve it! My team has been using my forked ArgumentCaptors for the last six months with a lot of success and I'd love to see them added to the main branch. |
minicuts
left a comment
There was a problem hiding this comment.
Great feature! Can you please add some godoc?
|
@codyoss the naming of ArgumentCaptor seems fine to me. Any other suggestions? |
* Added GoDoc * Removed the interface for ArgumentCaptor * Renamed (and exported) the struct to ArgumentCaptor * Renamed Value and AllValues to LastValue and Values respectively * Updated unit tests
* added some small grammar changes to the docs for LastValue and Values methods
* Updated test names to reflect the changes to the method names LastValue and Values
|
Thoughts on just calling it |
| } | ||
|
|
||
| // AnyCaptor is a helper method that returns a new *ArgumentCaptor struct with the matcher set to an anyMatcher | ||
| func AnyCaptor() *ArgumentCaptor { |
There was a problem hiding this comment.
This would be ok with me if the call usually happened in the EXPECT line, but because captors aren't that useful unless they're declared in advance, I think it makes more sense to keep it as is and view it like a constructor.
Here's an example:
// here we don't have a way to grab the values
mockDao.EXPECT().InsertIDs(gomock.CaptureAny()).Times(1)
// we need to store the captor first
idCaptor := gomock.AnyCaptor()
mockDao.EXPECT().InsertIDs(idCaptor).Times(1)
actualIDs := idCaptor.LastValue().([]int)|
@tylersammann Thanks for working on this! |
|
@codyoss I like the name |
|
@nguyenfilip @codyoss I think I've addressed most of the PR comments and change requests. Let me know how you feel re: |
|
Thanks, sorry for the delay. I will get back to review this by the end of the week. |
|
@tylersammann I don't have any more comments. I will let @codyoss approve because there is stil the discussion open about naming. |
|
@nguyenfilip @codyoss this should be ready for another look. I've reverted the method names back to |
codyoss
left a comment
There was a problem hiding this comment.
Did a quick review mostly of the docs. Will come back and add some more this weekend.
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package gomock |
There was a problem hiding this comment.
Would you add a couple examples of both a simple and more complex case using this as an end-user.
|
@tylersammann Are you still interested in this change? |
|
Yep definitely @codyoss. I can address your review this weekend |
|
No worries, I still owe you another through review too. I apologize for not being more responsive here. These have been unprecedented times. |
|
I'll wait for you to review again before I push any changes. Thanks for looking @codyoss |
|
@codyoss I came back through and addressed your first review because I had some time. Feel free to take another look! |
|
Thanks everyone for the hard work. Any progress on this PR? |
|
Thanks folks. Can we get this merged? This'd be a great feature add. |
|
I'm happy to continue on this. It'll need a re-review from @codyoss and it's been a while, so a few tests and a merge conflict needs fixing. |
|
@tylersammann Apologies I dropped the ball on this PR. Let me take a look again here this week. One thing to consider for the future now is also generics with 1.18 coming soon. I would like to think about if generics might change surface. I will get back to you soon! |
|
@codyoss thanks! I agree, generics might remove the need for type casting captured values, and that would be a big win. |
|
I think I am going to close this for now. I believe there might be a better surface design for this given generics now. Let's keep the chat going in the issue though. |
Resolves Issue: #262
ArgumentCaptorstruct that composesMatcherArgumentCaptorArgumentCaptormethodsThe
Captor()method takes aMatcherinterface to make it as flexible as possible.AnyCaptor()is just there because it seems like theanyMatcheris probably going to be used with captors the most.