【问题标题】:Integration test - how to test a method with no inputs and a render集成测试 - 如何测试没有输入和渲染的方法
【发布时间】:2014-06-16 13:37:52
【问题描述】:

如何在没有输入和渲染视图的情况下集成测试 (Grails) 控制器?这是我的控制器代码和测试;测试看起来正确,但抛出“java.lang.Exception:没有找到匹配 grails 测试目标模式过滤器的测试”错误。非常感谢您的帮助。 -瑞安

控制器代码部分:

class ReportSiteErrorsController {

    static allowedMethods = [reportError: 'GET', saveReportError: 'POST']

    def mailService

    @Transactional(readOnly = true)
    def reportError() {
        render(view:'reportError', model:[]) 
    }
}

集成测试:

@TestFor(ReportSiteErrorsController)
class ReportSiteErrorsControllerTests extends DbunitGroovyTestCase {

    @Test
    void "test report error"() {
        controller.reportError()
        assert view == "reportError"
    }
}

【问题讨论】:

  • 您在尝试运行测试时执行了什么命令?
  • grails 测试应用集成:nameoftest.groovy
  • 是的,我不确定。我也检查了我所有的格式和输入语句。
  • 我希望你在项目中有一些愚蠢的东西。如果你可以在一个简单的应用程序中重现它,你可以推送到 github 或类似的地方,我很乐意看看。
  • 我不知道您所说的“我已经检查了所有格式”是什么意思。

标签: grails integration-testing


【解决方案1】:

在命令行中关闭扩展名。您应该指定测试的名称,而不是定义它的文件的名称。

使用类似...

grails test-app integration: NameOfTest

不是……

grails test-app integration: NameOfTest.groovy

希望对你有帮助。

【讨论】:

  • 杰夫,谢谢。我刚刚尝试过 - 运行了两次 - 不幸的是仍然失败。 "没有找到匹配 grails 测试目标模式过滤器的测试"
  • 这对我来说没有意义。如果您在github.com/jeffbrown/integrationtestexample 签出项目并运行grails test-app integration: Person,我希望它可以工作,如果您运行grails test-app integration: Person.groovy,我希望它不会工作。
  • 我想通了!盯着同一个屏幕看太多小时了——我在那里有一个 import spock.lang.Specification 行,所以我的测试出现了身份危机:我试图像 Spock 一样运行 Junit 测试。 :)
  • 这也没有任何意义。其他的东西一定已经改变了。只是在测试中拥有import spock.lang.Specification 没有任何影响。如果您不使用 Spock,它根本不会影响已编译的测试。它只是告诉编译器在编译时在哪里可以找到 Specificaiton 类,如果你引用它的话。如果您不引用它,则导入不会导致任何问题。如果你有一个通过的测试并重新添加 import 语句,测试仍然会通过。
【解决方案2】:

在 Grails 2.4 中,测试 render "hello" 的默认语法是:

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage     instructions
 */
@TestFor(SimpleController)
class SimpleControllerSpec extends Specification {

def setup() {
}

def cleanup() {
}

void "test something"() {
    controller.index()
    expect:
     response.text == "hello"       
}
}

assert 不适合我。另见:the Grails Documentation

【讨论】:

    猜你喜欢
    • 2017-05-20
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多