Men*_*los 2 grails unit-testing 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"
Run Code Online (Sandbox Code Playgroud)
我理解spock单元测试的基础知识,但我不理解这些1 *行.
我也遇到了多个错误,例如:
junit.framework.AssertionFailedError: Too few invocations for:
1 * controller.cService.getRegions() >> [] (0 invocations)
Unmatched invocations (ordered by similarity):
None
Run Code Online (Sandbox Code Playgroud)
你告诉Spock,有问题的方法必须被调用一次(1 * controller.cService.getRegions() >> []意味着,getRegions这个服务必须被调用一次(1 *)并返回一个空列表(>> [])).但事实并非如此.这是错误消息告诉你的(0 invocations).