0

我正在使用 gomock 编写测试用例,我尝试使用 gomock 的 EXPECT() 行为设置函数的参数。

这就是我想要做的。我正在尝试模拟签名如下所示的函数的行为:

func Post(arg1 interface{}, arg2 interface{}, arg3 interface{}, arg4 interface{}, response *ResultResponse) error {
       //This function does the following
       1. Do a http post call and and receive the response
       2. Parse the data received and store the result in response as json.Unmarshal('data received by post call', response)
       3. Return if any error occurred during the whole process.
}

我已经使用gomock为这个函数实现了模拟。我想实现动态返回行为。以下是我正在尝试做的事情。

mockBehavior := mockClient.EXPECT().Post(gomock.Any(), gomock.Any(),
        gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()

    mockBehavior.Do(func(arg1 interface{}, arg2 interface{}, arg3 interface{},
        arg4 interface{}, response *ResultResponse){

        mockBehavior.SetArg(4, getResultResponse()).Return(nil)

})


func getResultResponse() ResultResponse {
    nextPage = nextPageMap[nextPage]
    var resultResponse ResultResponse
        resultResponse = ResultResponse{
            Result: getResponse(nextPage),
            }

    return resultResponse
}

func getResponse(nextPage string) Response {
    return Response{
        Blocks:        getBlocks(),
        NextPageToken: nextPage,
    }
}


func getBlocks() []Blocks {
    blocks := []Blocks{
        {
            Block:       "Block1",
        },
        {
            Block:       "Block2",
        },
    }
    return blocks
}

var nextPage string = "Some initial constant value"

var nextPageMap = map[string]string{
//Map of some string values
}

我想在这里实现一个动态行为。根据 nextPage 的值,我想在每次调用 post 函数时设置不同的值。

我正在使用 Visual Studio 代码来运行测试用例。但是在执行 SetArg(4, getResultResponse()) 语句后运行测试用例时,我看到消息无法加载源''。 该参数的值未按预期设置。因此,我无法正确执行我的测试用例。无法理解这是什么原因

4

0 回答 0