grails mockFor关闭幽灵 [英] grails mockFor closure wierdness

查看:79
本文介绍了grails mockFor关闭幽灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对,

所以当我使用测试插件的mockFor方法设置我的模拟时,我期望一个返回null的方法
。如果我这样做

$ pre $ my code> myControl.demand.theMethod {return null}

在调试器中,我将'theMethod'调用结果设置为调试器中的某个闭包。



如果我这样做

  myControl.demand.theMethod { - >返回null} 

该值为空,与预期一致。

我不明白区别。 ...

解决方案

我希望我能说出正确的答案。

groovy文档 http://groovy.codehaus.org/Closures 它声明没有 - >封闭,即{},是一个含有一个参数的闭包,隐含地命名为it。 ....在某些情况下,您需要构建一个零参数的Closure,例如,使用GString进行模板化,定义EMC属性等等。您必须明确定义您的Closure为{ - >},而不是{}



实质上,你的模拟试图使用'return'作为参数。你需要 - >来说我没有参数可以传递,然后把你想要它返回到箭头的右边


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....

解决方案

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

这篇关于grails mockFor关闭幽灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆