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

4 views
Skip to first unread message

Barry Kaplan

unread,
Dec 14, 2009, 4:23:47 PM12/14/09
to Spock Framework
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.

Barry Kaplan

unread,
Dec 14, 2009, 4:28:24 PM12/14/09
to Spock Framework
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.

Peter Niederwieser

unread,
Dec 14, 2009, 4:40:08 PM12/14/09
to Spock Framework
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

Barry Kaplan

unread,
Dec 14, 2009, 5:04:03 PM12/14/09
to Spock Framework
Very very nice. Thanks!
Reply all
Reply to author
Forward
0 new messages