如何使用声明的类获取Geb模块实例?

时间:2016-04-12 14:18:11

标签: 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:没有方法签名:   geb.navigator.NonEmptyNavigator.verifySomething()适用于   参数类型:(geb.content.TemplateDerivedPageContent)值:   [whatever.ExamplePage - > theModule:whatever.ExampleModule]

我注意到同样的情况发生了但是从版本0.11开始有不同的类,异常消息如下:

  

groovy.lang.MissingMethodException:没有方法签名:   geb.navigator.NonEmptyNavigator.verifySomething()适用于   参数类型:(geb.content.SimplePageContent)值:[theModule -   SimplePageContent(owner:whatever.ExamplePage,args:[],value:   空)]

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

1 个答案:

答案 0 :(得分:4)

实现Navigator接口(包括从Module扩展的类)并从内容定义返回的对象包含TemplateDerivedPageContent个对象,这些对象委托给底层对象,但也允许生成{ {3}}

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

幸运的是有一种解决方法 - 如果你想强烈输入消耗模块的代码,那么使用getter而不是内容定义来声明它们。在你的情况下,它将是:

class ExamplePage extends Page {

    ExampleModule getTheModule() {
        module ExampleModule
    }

}