groovy - 如何获取 Geb 模块实例及其声明的类?

标签 groovy spock geb

直到 Geb 版本 0.10,下面的示例代码运行良好:

package whatever

import geb.Module
import geb.Page
import geb.spock.GebSpec

class ExampleSpec extends GebSpec {

    def 'MODULE - Y U NO HAVE THE RIGHT CLASS?'() {

        when:
            ExamplePage page = to ExamplePage

        then:
            verifySomething(page.theModule)
    }

    boolean verifySomething(ExampleModule module) {
        // ...
    }
}

class ExamplePage extends Page {

    static content = {
        theModule { module ExampleModule }
    }
}

class ExampleModule extends Module {

}

我想升级到最新的 0.13.1 但显然已经引入了破坏性(我会说是倒退)更改,结果如下:

groovy.lang.MissingMethodException: No signature of method: geb.navigator.NonEmptyNavigator.verifySomething() is applicable for argument types: (geb.content.TemplateDerivedPageContent) values: [whatever.ExamplePage -> theModule: whatever.ExampleModule]



我注意到自 0.11 版以来发生了同样的情况,但使用了不同的类,异常消息如下:

groovy.lang.MissingMethodException: No signature of method: geb.navigator.NonEmptyNavigator.verifySomething() is applicable for argument types: (geb.content.SimplePageContent) values: [theModule - SimplePageContent (owner: whatever.ExamplePage, args: [], value: null)]



为什么用给定的特定类声明的模块在运行时会丢失它?如何防止这种情况?

最佳答案

实现 Navigator 的对象接口(interface)(包括从 Module 扩展的类)和从内容定义返回的接口(interface)用 TemplateDerivedPageContent 包装委托(delegate)给底层对象但也允许产生 meaningful path to the object for error reporting 的对象.

模块的包装在旧版本的 Geb 中工作,然后它被无意中删除,现在又回来了。即使您仍然可以在包装时调用模块的所有方法,这要归功于 TemplateDerivedPageContent在像您这样的情况下动态委派给您遇到麻烦的底层对象 - 当您想要强烈键入使用模块的代码时。因此,我仍然不确定我们应该在这里牺牲什么——更好的错误报告或强类型的能力,并且这种包装可能会在 Geb 的 future 版本中被删除。

幸运的是,有一个解决方法——如果你想对使用模块的代码进行强类型化,那么使用 getter 而不是内容定义来声明它们。在您的情况下,它将是:

class ExamplePage extends Page {

    ExampleModule getTheModule() {
        module ExampleModule
    }

}

关于groovy - 如何获取 Geb 模块实例及其声明的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36576006/

相关文章:

java - 如何通过JMX获取James Server中注册的用户列表

unit-testing - 如何在Spock单元测试中测试Groovy构造函数并覆盖100%的代码

image - 在 Gradle 中使用 Spock 测试创建的文件

geb - WebElement clear 函数在某些情况下不会清除输入值

java - Groovy 将装箱类型传递给 Jenkins,如何拆箱?

jenkins - 如何发现Jenkins构建的其他原因?

xml - Groovy+ XML : handle Attributes with Namespace-Prefix

java - 无法使用 JDK 10.0.2 使用 maven spock 执行目标

jquery - Geb 在空导航器上使用导航器方法

testing - Grails 功能测试 - 数据库设置/拆卸,在 Eclipse 中作为 JUnit 运行