0

我想将我的单元测试函数中的测试报告数据设置为即时模拟,GetTestFunc但实际上GetTestFunc只返回error并设置我的指针变量TestReport

但是在使用模拟时,我们正在返回,但指针变量的设置没有发生,所以我想在TestReport指针变量中分配预期值,这样我就可以为方法的其他路径编写 Ut。

使用“github.com/stretchr/testify/mock”

-- model.go --

type TestReport struct {
    Id           string 
    Data         string 
    ReportData   string 
}

-- get_data_handler.go --

func (t *test) GetTestReport(xyz string) interface{} {
    var testReport model.TestReport

    err := t.db.GetTestFunc(table_name, "id", xyz, &testReport)
    var report map[string]interface{}
    if err == nil {
        fmt.Println(testReport)
        var resultReportData map[string]interface{}
        reportDataByte := []byte(testReport.Data)

    }
}

-- get_data_handler_test.go --

func (db *MockedDb) GetTestFunc(table, key, Value string, out interface{}) error {
    args := db.Called(table, Key, Value, out)
    return args.Error(0)
}

func TestGetTestReport(t *testing.T) {
    testReport := model.TestReport{
        Id:        "123456789",
        FirstName: `[{"id":454866976,"newid":"123456789"}]`,

        ReportData: `{
                "Id": "test1",
                "Data": [{
                    "Id": 454866976,
                    "moredata": [{
                            Value": "12345"
                        },
                        {
                            "text": "abc",
                            "value": "12345678"
                        }
                    ]
                }]
            } Status:Available}`,
    }
    mockedDbObj.On("GetTestFunc", mock.Anything, mock.Anything, mock.Anything, mock.AnythingOfType("*model.TestReport")).Return(nil, testReport).Run(func(args mock.Arguments) {
        fmt.Println("inside run $$$$$$$$$$$$$$$$$$$$$$$$$$$$")
        fmt.Println(args.Get(3))
        arg := args.Get(3).(*model.testReport)
        arg = &testReport

        fmt.Println("end $$$$$$$$$$$$$$$$$$$$$$$$$$$$")
    })
}
4

0 回答 0