2013-12-17 27 views

回答

10

当你模拟一个方法时,你可以指定一个范围,表示你可以调用该方法的次数。从文档摘自:

You then specify the name of the method that you want to mock with an optional range as its argument. This range determines how many times you expect the method to be called, so if the number of invocations falls outside of that range (either too few or too many) then an assertion error will be thrown. If no range is specified, a default of "1..1" is assumed, i.e. that the method must be called exactly once.

control.demand.scp(2..2){ file, todir, verb, pass -> return "" } 

我无法找到错误的原因,所以我希望这可以帮助别人。

+6

这是严格的嘲弄。您必须指定方法将按需调用模拟对象的次数。你可以通过传递一个标志[mockFor指示松散的嘲弄]来绕过严格的模拟(http://grails.org/doc/latest/guide/testing.html#mockingCollaborators)。 – dmahapatro

+0

感谢您的提示dmahapatro,我发现错误是关于的后,我意识到国旗,但是当它发生时,我无法弄清楚是什么造成了它。当你不关心该方法被调用多少次时,这是一个很好的选择。谢谢! – alscu