Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails mockFor closure wierdness

Tags:

grails

groovy

Right,

so when I set up my mock using the testing plugin's mockFor method, I expect a method that returns null. If I do

myControl.demand.theMethod {return null}

in the debugger, the value that I set the 'theMethod' call result to is some closure in the debugger.

If I do

myControl.demand.theMethod {->return null}

the value is null, as expected.

I dont understand the difference....

like image 597
hvgotcodes Avatar asked May 27 '10 16:05

hvgotcodes


1 Answers

I hope I word this right

In the groovy documentation http://groovy.codehaus.org/Closures it states that "A Closure without -> , i.e. {} , is a Closure with one argument that is implicitly named as 'it'." .... "In some cases, you need to construct a Closure with zero arguments, e.g. using GString for templating, defining EMC Property etc. You have to explicity define your Closure as { -> } instead of just { }"

In essence, your mock was trying to use 'return' as an argument. You need the -> to say "I have no parameters to pass" and then put what you want it to return on the right side of the arrow

like image 196
Jen Avatar answered Oct 13 '22 18:10

Jen