直到 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/