Is it possible for a mocked method to return its argument?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Is it possible for a mocked method to return its argument?

Barry Kaplan (2)
Something like

    builder.transitionTo(state) >> state

The second state is not declared at runtime

I've also tried things like

    def state
    builder.transitionTo({state = it}) >> state

But this returns null.

--

You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is it possible for a mocked method to return its argument?

Barry Kaplan (2)
I supposed I should mention that the instance "state" is created by
the code under tests -- the test itself cannot create or initialize
this value.

--

You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is it possible for a mocked method to return its argument?

Peter Niederwieser
Administrator
Hi Barry,

yes it's possible. The result generator is passed the list of
arguments for the invocation. Hence you can do: builder.transitionTo
(_) >> { it[0] }

Your way of capturing 'state' should also work. You just have to defer
evaluation of the result:

def state
builder.transitionTo({state = it; true}) >> { state }

Inserting 'true' makes sure that the interaction always matches.
Otherwise it would only match if 'state' evaluated to
'true' (according to Groovy truth).

Hope this helps.

Cheers,
Peter



On Dec 14, 10:28 pm, Barry Kaplan <[hidden email]> wrote:
> I supposed I should mention that the instance "state" is created by
> the code under tests -- the test itself cannot create or initialize
> this value.

--

You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is it possible for a mocked method to return its argument?

Barry Kaplan (2)
Very very nice. Thanks!

--

You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.


Loading...