为什么要“得到”呢;groovy.lang.MissingMethodException:没有方法的签名;在spock中何时使用方法调用?

为什么要“得到”呢;groovy.lang.MissingMethodException:没有方法的签名;在spock中何时使用方法调用?,groovy,spock,Groovy,Spock,我有一个集成测试。其中的一些代码: Test1 { static closure1 = { Class1 resultState, Class1 originState -> with(resultState) { name == originState.name breed == originState.breed bornDate == originS

我有一个集成测试。其中的一些代码:

Test1 {
        static closure1 = { Class1 resultState, Class1 originState ->
            with(resultState) {
                name == originState.name
                breed == originState.breed
                bornDate == originState.bornDate
            }
        }

        @Unroll
        def 'exercise #methodName'() {
            ...
            expected(resultState, originState)
            ...
            methodName  || expected
            'name1'     || closure1
        }
}
我得到了一个groovy.lang.MissingMethodException:没有方法的签名:Test1$\uuu clinit\uu closure10.with()适用于参数类型:(Class1,Test1$\uu clinit\uu closure10$\u closure12)值:[//值]

但当我用方法而不是闭包重构代码时,一切都很好

    Test1 {

            void method1(Class1 resultState, Class1 originState) {
                with(resultState) {
                    name == originState.name
                    breed == originState.breed
                    bornDate == originState.bornDate
                }
            }

            @Unroll
            def 'exercise #methodName'() {
                ...
                expected(resultState, originState)
                ...
                methodName  || expected
                'name1'     || closure1
             }


        }
但是为什么呢??在closure from中,我使用block获取异常,而不是closure调用。 在我的另一种测试形式中,使用闭包(但不使用块)的测试效果很好。这里的闭包类型-闭包,在其他测试中-闭包,如果重要的话


我的代码出了什么问题?

一个问题是
with
规范
上的一个方法,但是您在静态上下文中拥有它,因此在这一点上,没有
with
方法来调用

另一个原因是,这超出了spec方法的范围,因此不会断言
a==b
行,因为spock dsl不会拾取它们

一种解决方案是将其设为一个方法,并传入一个方法句柄,断言您的值,如果它们都通过,则返回true:

private test1(resultState, originState) {
    with(resultState) {
        assert name == originState.name
        assert breed == originState.breed
        assert bornDate == originState.bornDate
    }
    true
}

@Unroll
def 'exercise #methodName'() {

    ...
    then:
    expected(resultState, originState)

    where:
    methodName  || expected
    'name1'     || this.&test1
}

你能发布一个完整的测试类,这样我们就可以重现相同的异常吗?您发布的代码没有实现我使用的方法解决方案,但问题是在闭包的静态上下文中。非常感谢。