使用带有参数的Geb静态内容DSL时的IntelliJ类型错误 [英] IntelliJ type error when using Geb static content DSL with parameters

查看:60
本文介绍了使用带有参数的Geb静态内容DSL时的IntelliJ类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我使用带有参数的静态内容定义的闭包时,IntelliJ都会抱怨类型不匹配(即使我在闭包内指定了类型).

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) }
}

以上两项均可在我的测试中成功使用,但是如果我要在其中一项测试中使用"namedModule",则如下:

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将以黄色突出显示参数"moduleName",并显示以下错误:

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

'namedModule'不能应用于'(java.lang.String)'

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

我们正在尝试以某种方式重构测试,这意味着您可以更轻松地浏览代码(例如,避免IntelliJ无法解决的任何Geb/Groovy魔术"),这是最后剩下的问题之一这是不可能的.

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.

推荐答案

这是IntelliJ中对Geb支持的已知限制.IntelliJ始终将内容定义视为页面和模块的属性,即使可以对其进行参数化.鉴于IntelliJ中的Geb支持是开源的,我们可能可以添加对此的支持.

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.

同时,作为一种变通办法,您可以使用参数化内容的方法而不是内容定义,并且IntelliJ将能够理解它们并能够对其进行重构:

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:

  • 您将失去使用内容定义选项的功能.如果您需要将这些内容用于内容定义,那么我建议您创建一个参数化的私有"内容定义(例如,在名称的开头加上 _ ),您将永远只能从页面或模块
  • 即使返回的内容为空,也不会引发
  • RequiredPageContentNotPresent .要解决此问题,您将需要为每种此类方法添加手动验证,或者使用第一个项目符号中概述的策略以及私有"内容定义
  • 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

这篇关于使用带有参数的Geb静态内容DSL时的IntelliJ类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆