IntelliJ type error when using Geb static content DSL with parameters

白昼怎懂夜的黑 提交于 2021-01-28 07:03:16

问题


Whenever I use a static-content defined closure that takes parameters, IntelliJ will complain that the types do not match (even if I specify the type inside the closure).

For example, if I use this static content block:

static content = {
        myModule { $('myModule').module(CustomModule) }
        namedModule { String name -> $(".$name").module(CustomModule) }
}

Both of the above items can be used successfully in my tests, but if I was to use 'namedModule' in one of my tests as follows:

page.namedModule("moduleName").moduleMethod("blah blah blah")

IntelliJ will highlight the parameter "moduleName" in yellow with the error:

'namedModule' cannot be applied to '(java.lang.String)'

We are trying to refactor our tests in a way that means you can navigate through the code easier (e.g. avoiding any Geb/Groovy 'magic' that IntelliJ can't resolve), and this is one of the last remaining issues preventing this from being possible.


回答1:


This is a known limitation to Geb support in IntelliJ. IntelliJ always treats content definitions as properties of pages and modules even though they can be parametrised. Given that Geb support in IntelliJ is open sourced we could probably add support for this.

In the mean time, as a workaround you can use methods for parametrised content instead of content definitions and IntelliJ will be able to understand these and be able to refactor them:

void namedModule(String name) {
    $(".$name").module(CustomModule)
}

There are some caveats, though:

  • you will loose ability to use content definition options; if you need to use these for a content definition then I suggest creating a parameterised "private" content definition (for example with a _ at the beginning of the name) that you will only ever access from within the page or module
  • RequiredPageContentNotPresent will not be thrown even if the returned content is empty; to work around it you will either need to add manual verification to each such method or use a strategy outlined in the first bullet point with using "private" content definitions


来源:https://stackoverflow.com/questions/44763703/intellij-type-error-when-using-geb-static-content-dsl-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!