是否可以创建 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"
}
有人可以帮我更换
?????
用一段代码来解决这个问题?提前致谢。
最佳答案
是的,这里有几个选项:
emptyPagedResultList
- 见 FooControllerSpec.groovy line 11举个例子Mock()
作为 emptyPagedResultList
- 见 FooControllerSpec.groovy line 25举个例子关于unit-testing - Grails 3.1.7 单元测试中的 PagedResultList 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38225537/