【问题标题】:Grails controller unit tests pass on the command line and not in IntellijGrails 控制器单元测试通过命令行而不是 Intellij
【发布时间】:2014-07-02 17:37:51
【问题描述】:

我正在使用 Grails 2.3.8,最近我开始修复一些过时的测试,这些测试在过去的几个 Grails 2.3.x 版本中停止工作。

在 Spock 控制器测试期间,以及从控制器响应域实例时,针对模型的断言失败/通过取决于测试执行环境。具体来说,当使用 Grails 控制台与 IntelliJ (Junit) 执行测试时,模型上的域实例具有不同的属性名称。

我一直在通过在我的 Spock 规范中的 thenexpect 块中做出这种类型的断言来对冲它:

void "show action correctly handles a valid instance"() {

    given: "a valid domain instance"

        def myDomainObject = MyDomainClass.build()

    when: "calling the show action with a valid domain instance"

        controller.show(myDomainObject)

    then: "respond to the show view with the domain instance set on the model"

        view == ‘show’
        // the property name ends with ‘Instance’ in one env and not in the other                        
        model.myDomainObjectInstance ?: model.myDomainObject == myDomainObject
}

【问题讨论】:

  • 投反对票的人能解释一下原因吗?不想争论,但我想知道我的帖子中是否有任何错误或遗漏,可能有助于其他人理解和回应。
  • 我会仔细检查您在两种环境中使用的 grails 版本是否相同。很容易以不匹配而告终。
  • 我希望它像那样微不足道,但我感觉它与 Intellij 中 Grails 环境的配置方式有关。在我们的生产环境中一切运行良好。一旦我在下一个 Sprint 中获得一些时间,我将尝试找到问题的根源。
  • 有这方面的消息吗?我遇到了完全相同的问题。我也不想破解我的测试代码来完成这项工作。出于这个原因,我一直在通过 Intellij 中的命令行界面运行测试。

标签: grails groovy junit intellij-idea spock


【解决方案1】:

不确定这是否是个问题,但我认为您应该将控制器的响应保存在变量中,并使用 renderArgs 变量访问视图:

void "显示动作正确处理一个有效实例"() {

given: "a valid domain instance"

    def myDomainObject = MyDomainClass.build()

when: "calling the show action with a valid domain instance"

    def model = controller.show(myDomainObject)

then: "respond to the show view with the domain instance set on the model"

    renderArgs.view == ‘show’
    // the property name ends with ‘Instance’ in one env and not in the other                        
    model.myDomainObjectInstance ?: model.myDomainObject == myDomainObject

}

不确定是不是这个问题。

【讨论】:

  • 模型不为空且包含实例,断言view == 'show'通过。问题是从命令行运行时,用于访问实例(在模型上)的属性名称是“myDomainObject”,但在 IntelliJ 中运行时,它是“myDomainObjectInstance”。就目前而言,有几十个这样的测试通过,但这只是因为我明确断言“myDomainObject”等于“model.myDomainObjectInstance”“model.myDomainObject”。我不清楚为什么会发生这种情况。
  • 我会尝试检查 intellij 项目中的库和命令行选项之间的差异
  • 根据我上面的评论,我想我倾向于这样。
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多