dongliping003116 2017-09-22 22:43
浏览 80

如何在golang中的for循环中模拟相同的输入和不同的返回值

I'm running a test with multiple parameters in a for loop using go lang testing.

I ran into a situation where same return value (and first set) is returned every time the mock is called. What I want to be able to do is change the return value for each test when the input is same i.e., same On but different Return in a loop.

I am using stretchr/testify for mocks. It looks like it will not overwrite already created mock when On is same.

func TestUpdateContactWithNewActions(t *testing.T) {
    tests := []struct {
        testName  string
        getParams func() *activities.UpdateContactWithNewActionsActivity
        mockError error
    }{

        {"UpdateContactWithNewActions with error from contact service",
            func() *activities.UpdateContactWithNewActionsActivity {
                return fixtures.GetUpdateContactWithNewActionsActivity()
            }, fixtures.Err},
        {"UpdateContactWithNewActions valid",
            func() *activities.UpdateContactWithNewActionsActivity {
                return fixtures.GetUpdateContactWithNewActionsActivity()
            }, nil},
    }

    lib.LoadWithMockClients()

    for _, test := range tests {
        test := test
        t.Run(test.testName, func(t *testing.T) {
            lib.MockCSClient.On(
                "UpdateContactWithNewActions",
                mock.AnythingOfType("tchannel.headerCtx"),
                fixtures.UpdateContactWithNewActions).Return(test.mockError)

            returnedResult, err := test.getParams().Execute(fixtures.Ctx)
            if test.mockError == nil {
                // some assertion
            }
            assert.Error(t, err)
        })
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)