简体   繁体   English

Grails单元测试模拟服务返回无效的对象

[英]Grails unit test mock service return invalid object

I am creating unit test for this (working fine) Grails service : 我正在为此Grails服务创建单元测试:

class CommonCodeService {

    def gridUtilService

    def getList(def params){
        def ret = null

        try {
            def res = gridUtilService.getDomainList(CommonCode, params)
            def rows = []
            def counter = 0
            res?.rows?.each{ // this is line 15
                rows << [
                        id: counter ++,
                        cell:[

                            it.key,  
                            it.value  
                        ]
                ]
            }

            ret = [rows: rows, totalRecords: res.totalRecords, page: params.page, totalPage: res.totalPage]

        } catch (e) {
            e.printStackTrace()
            throw e
        }

        return ret
    }

}

This a method from collaborator GridUtilService : 这是协作者GridUtilService一种方法:

import grails.converters.JSON

class GridUtilService {

    def getDomainList(Class domain, def params){
        /* some code */
        return [rows: rows, totalRecords: totalRecords, page: params.page, totalPage: totalPage]
    }
}

And this is my (not working) unit test for it : 这是我(不起作用)的单元测试:

import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import com.emerio.baseapp.utils.GridUtilService

@TestFor(CommonCodeService)
@Mock([CommonCode,GridUtilService])
class CommonCodeServiceTests {

    void testGetList() {
        def rowList = [new CommonCode(key: 'value', value: 'value')]
        def serviceStub = mockFor(GridUtilService)
        serviceStub.demand.getDomainList {Map p -> [rows: rowList, totalRecords: rowList.size(), page:1, totalPage: 1]}
        service.gridUtilService = serviceStub.createMock()
        service.getList() // this is line 16
    }

}

When I run the test it shows exception : 当我运行测试时,它显示异常:

No such property: rows for class: com.emerio.baseapp.CommonCodeServiceTests
groovy.lang.MissingPropertyException: No such property: rows for class: com.emerio.baseapp.CommonCodeServiceTests
    at com.emerio.baseapp.CommonCodeService.getList(CommonCodeService.groovy:15)
    at com.emerio.baseapp.CommonCodeServiceTests.testGetList(CommonCodeServiceTests.groovy:16)

It seems the mocked GridUtilService returns CommonCodeServiceTests instance instead of Map . 看来, GridUtilService返回CommonCodeServiceTests实例而不是Map What is wrong with my unit test? 我的单元测试有什么问题?

It looks like you need to fix your method params for the mocked getDomainList() call. 看来您需要修复getDomainList()调用的方法参数。 You have it as Map m , but it probably needs to be Class c, Map m . 您拥有Map m ,但它可能需要是Class c, Map m

From docs , 文档中

The closure arguments must match the number and types of the mocked method, but otherwise you are free to add whatever you want in the body. 闭包参数必须与模拟方法的数量和类型匹配,但否则,您可以随意在主体中添加任何内容。

Why a miss on the arguments behaves the way it does is a stumper. 为什么会遗漏参数,这是一个绊脚石。 I can replicate your problem using my own stripped-down classes. 我可以使用自己的精简类来复制您的问题。 I also found that the returned type for a call on the method when there's a param miss is a closure of the test class, which, for my simple case at least, can then be .call() 'ed to get the desired (mocked) result. 我还发现,在发生参数遗漏的情况下,方法调用的返回类型是测试类的关闭,至少在我的简单情况下,它可以是.call()以得到所需的(模拟的) )结果。 I'm not sure if this behavior supports some kind of functionality or is a bug. 我不确定这种行为是否支持某种功能或是否有错误。 It's certainly confusing. 这肯定令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM