Grails Spock单元测试 - 什么是“1 *”以及为什么“调用太少”?

时间:2015-03-04 21:25:29

标签: unit-testing grails spock

我在单元测试中看到以下代码,用于我从其他人继承的控制器:

  when: "When the controller executes a registration"
      controller.index()

    then: "the signup should show registration again"
      1 * controller.cService.getRegions() >> [] 
      1 * controller.dService.chkAvail(_) >> "AVAILABLE"
      1 * controller.uService.createUser(_) >> { a-> throw new RuntimeException("Roll me back!")}
      1 * controller.pService.registerPayMethod(_) >> { cc-> true }
      view == "/signUp/su"

我理解spock单元测试的基础知识,但我不理解这些1 *行。

我也遇到了多个错误,例如:

junit.framework.AssertionFailedError: Too few invocations for:

1 * controller.cService.getRegions() >> []   (0 invocations)

Unmatched invocations (ordered by similarity):

None

1 个答案:

答案 0 :(得分:5)

你告诉Spock,有问题的方法必须被调用一次(1 * controller.cService.getRegions() >> []意味着,此服务的getRegions必须被调用一次(1 *)并将返回一个空列表(>> []))。但事实并非如此。这是错误消息告诉您的内容(0 invocations)。