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

How to capture and return value? #16

Closed
bulldetektor opened this issue Mar 24, 2017 · 6 comments
Closed

How to capture and return value? #16

bulldetektor opened this issue Mar 24, 2017 · 6 comments
Assignees
Milestone

Comments

@bulldetektor
Copy link

bulldetektor commented Mar 24, 2017

I'm trying to do both a capture and return on the same mock, but I can't figure out how to set it up. This is my attempt so far:

const mockedDS = mock(MyDataService);
const whenCall = when(mockedDS.update(anything(), anything());
whenCall.thenReturn("OK");
const firstCaptor = new Captor<Array<Employee>>();
const secondCaptor = new Captor<TimePeriod>();
whenCall.thenCapture(firstCaptor, secondCaptor);

It seems that I can only do one or the other? Depending on whether I call thenCapture before thenReturn or vice versa it will only capture OR return the value.

Btw. What I'd really like to write is this;

const mockedDS = mock(MyDataService);
const firstCaptor = new Captor<Array<Employee>>();
const secondCaptor = new Captor<TimePeriod>();
when(mockedDS.update(anything(), anything())
    .thenCapture(firstCaptor, secondCaptor);
    .thenReturn("OK");

...but both thenReturn and thenCapture returns void

@NagRock
Copy link
Owner

NagRock commented Mar 28, 2017

Thanks for reporting, I will add this functionality soon. Currently you can use thenCall to capture and return value as workaround.

@bulldetektor
Copy link
Author

Yes, that's what I ended up doing. Works fine, just not as fluent as I'd wish it was. But apart from that; ts-mockito is just lovely to work with :)

@NagRock
Copy link
Owner

NagRock commented Mar 28, 2017

Thanks for appreciation. As I wrote earlier I will add functionality to capture and stub mock with API that you tried to use: when(mock.smthn(anything())) .thenCapture(firstCaptor); .thenReturn("something");

@NagRock
Copy link
Owner

NagRock commented Apr 28, 2017

Should be fixed in 2.0 version. Please reopen if something is still wrong.

@alexleen
Copy link

alexleen commented Jul 21, 2020

Should be fixed in 2.0 version. Please reopen if something is still wrong.

What was the end result? I'm using 2.6.1 and can't seem to find a way to do this. What I would like is something similar to your comment above: when(mock.smthn(anything())).thenCapture(firstCaptor).thenReturn(firstCaptor);
i.e. I'd like to return whatever was passed as the argument.

@matitalatina
Copy link

@alexleen I had your issue and I ended up using https://github.com/NagRock/ts-mockito/#custom-function
Just create a function that returns the first argument.

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

4 participants