unit-testing - Grails 3.1.7 单元测试中的 PagedResultList 实例

标签 unit-testing grails grails-3.0 grails-3.1

是否可以创建 PagedResultList 实例或 Mock?

背景:目前我正在编写 Controller 单元测试。如果有必要,我会 stub 服务函数调用。但是其中一些函数有PagedResultList作为返回类型。所以我必须注入(inject) PagedResultList实例或 null .

在某些情况下,我需要一个实例,因为 Controller 会执行以下操作:

testFunction(){
    def result = sampleService.doSomething()

    if (result.empty) {
        variable = "it´s empty"
    }
    render variable
}

我的测试看起来像这样:
void "sample Test"(){
    given:
        controller.sampleService = Mock(SampleService)
        PagedResultList emptyPagedResultList = ?????

    when:
        controller.testFunction()

    then:
        1 * controller.sampleService.doSomething() >> emptyPagedResultList
        response.text == "it´s empty"
}

有人可以帮我更换?????用一段代码来解决这个问题?

提前致谢。

最佳答案

是的,这里有几个选项:

  • 您可以使用真正的 PagedResultList 作为 emptyPagedResultList - 见 FooControllerSpec.groovy line 11举个例子
  • 你可以使用另一个 Spock Mock()作为 emptyPagedResultList - 见 FooControllerSpec.groovy line 25举个例子
  • 关于unit-testing - Grails 3.1.7 单元测试中的 PagedResultList 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38225537/

    相关文章:

    grails - Grails脚手架模板-从域类获取属性

    asp.net-mvc - MvcIntegrationTestFramework 或针对 ASP.NET MVC 3 更新的替代方案

    c++ - 测试集合的相等性而不显示不匹配

    Grails:如何同时测试和开发插件?

    grails - 用Grails实现OAuth2提供程序

    grails - Geb在Grails应用中发现错误的<title>标签

    java - 抽象测试类

    java - 使用 java 所有者 aeonbits 进行测试

    templates - 如何在Grails 3.x中创建可重用的布局和脚手架模板

    grails - Grails 3.0.10无法解析MockFor