如何在Spock中指定动态调用目标断言

问题描述 投票:0回答:1

考虑这个例子

@AllArgsConstructor
class Foo{
  private AHandler ahandler;
  private BHandler bhandler;


  public void handle(String what){

     if(what.equals("A")){
          ahandler.handle(what);
     }else if(what.equals("B"){
       bhandler.handler(what)
     }else throw new RuntimeException();
  }
}

现在我尝试测试 if 逻辑,其中必须调用给定的输入正确的处理程序,而不是其他任何东西,但是我不知道如何动态指定断言。以下不起作用(但我认为表明了意图)

def ahandler=Mock(AHandler);
def bhandler=Mock(BHandler);
def service=new Foo(ahandler,bhandler);

when:
   service.handle(input);
then: 
   1 * expectedCalls
   0 * _ //no other calls
where:
input | expectedCalls
"A" | ahandler.handle("A")
"B" | bhandler.handler("B")

Spock 可以做这样的事吗?

java groovy spock
1个回答
0
投票

像往常一样,经过一些尝试和错误(以及谷歌搜索和检查 github spock 问题),我找到了令人满意的解决方案。

then: 
   1 * this."$handler".handle(_)
   0 * _ //no other calls
where:
input | handler
"A" | "ahandler"
"B" | "bhandler"
© www.soinside.com 2019 - 2024. All rights reserved.