Skip to content
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

Support multiple capture invocations #139

Closed
marchaos opened this issue Oct 5, 2016 · 8 comments
Closed

Support multiple capture invocations #139

marchaos opened this issue Oct 5, 2016 · 8 comments

Comments

@marchaos
Copy link
Contributor

marchaos commented Oct 5, 2016

Hey,
I'm pretty sure this currently isn't supported, but it would be nice to allow for captures to capture multiple calls to the same method. See this example of mockito:

http://stackoverflow.com/questions/5981605/can-mockito-capture-arguments-of-a-method-called-multiple-times

I guess the API would need something similar where you can call capture.getAllValues()

@marchaos
Copy link
Contributor Author

marchaos commented Oct 5, 2016

Please see https://github.com/marchaos/testdouble.js/blob/master/src/matchers/captor.coffee. This works for me. If there is only one invocation, the captor.value contains the captured value, otherwise captor.value is an array of all calls in the order the occurred. This allows for some backwards compatibility.

@searls
Copy link
Member

searls commented Oct 6, 2016

I'm struggling to imagine very many examples where this would be needed. Can you think of a not-far-fetched example?

@marchaos
Copy link
Contributor Author

marchaos commented Oct 6, 2016

It's a common pattern in streaming applications. I have a callback that is called whenever a message is received from a streaming source.

In my usecase, I mock multiple messages of different types to ensure the underlying code handles those accordingly. This needs to be occur in 1 unit test as messages come in on batch. In my captor, I catch each processed message and assert that they contain the correct values.

@searls
Copy link
Member

searls commented Oct 7, 2016

Okay. I think I can get behind adding a values attribute that shows all captured invocations and leave value as whatever the most-recent one was. That seems reasonable to me, since it wouldn't break back-compat. Do you agree?

@marchaos
Copy link
Contributor Author

marchaos commented Oct 8, 2016

Hi Justin, yep that sounds like a plan. Do you want me to create a PR for this?

@searls
Copy link
Member

searls commented Oct 12, 2016

Yeah, please feel free

@marchaos
Copy link
Contributor Author

#144

@searls
Copy link
Member

searls commented Dec 14, 2016

This landed in testdouble@1.8.0 for anyone keeping score.

@searls searls closed this as completed Dec 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants