gpt4 book ai didi

grails - Grails Spock测试特定异常

转载 作者:行者123 更新时间:2023-12-02 15:48:49 25 4
gpt4 key购买 nike

我正在使用Grails 2.4.5,努力使Grails描绘出不同的异常类型。

假设我要模拟以下内容:

class FooController { 
def barService
...
def fooAction() {
try {
barService.someMethod(params)
} catch(e) {
if (e instanceof FooException) { ... }
else if (e instanceof BarException) { ... }
else { ... }
}
}

给定下面的测试
    @TestFor(FooController)
class FooControllerSpec extends Specification {
def setup() { controller.barService = Mock(BarService) }
void "test"() {
given: "a mock dependency"
1* controller.barService.someMethod(_) >> { -> throw FooException('foo') }

when: "the action is requested"
controller.fooAction()

then: "expect the FooException behaviour from the action"
// some behaviour
}

我希望在模拟依赖项关闭中抛出FooException

但是,调试将显示以下内容:
groovy.lang.MissingMethodException: No signature of method: somePackage.FooControllerSpec$_$spock_feature_0_9_closure14.doCall() is applicable for argument types: (java.util.Arrays$ArrayList) values: [[[:]]]

这是一个错误吗?有没有办法以上述方式模拟不同的异常?

最佳答案

您需要在then块中指定行为,即:

void "test"() {
when: "the action is requested"
controller.fooAction()

then: "expect the FooException behaviour from the action"
1 * controller.barService.someMethod(_) >> { -> throw new FooException('foo') }
response.redirectedUrl == "/some/other/path"
}

关于grails - Grails Spock测试特定异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42831578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com