简体   繁体   English

如何在 go-sqlmock 中添加多个结果集?

[英]How can I add multiple result sets in go-sqlmock?

I have a db query that returns 2 result sets and I would like to unit test the go function that performs this query.我有一个返回 2 个结果集的 db 查询,我想对执行此查询的 go 函数进行单元测试。 While I can add and test rows like this:虽然我可以像这样添加和测试行:

myMockRows:=sqlmock.NewRows([]string{"col1","col2"}).AddRow("col1val1", "col2val2")
mock.ExpectQuery("my_stored_procedure").WithArgs(arg1, arg2).WillReturnRows(myMockRows)

I am not sure how to proceed with creating multiple result sets in my rows object.我不确定如何继续在我的rows对象中创建多个结果集。 How do I do that?我怎么做?

I had tried out, @Nikhil Vandanapu's answer and I wasn't getting the desired output.我已经尝试过@Nikhil Vandanapu 的回答,但没有得到想要的输出。 For some reason it took only myMockRows and myMockRows2 was ignored.出于某种原因,它只使用了myMockRowsmyMockRows2被忽略了。 I did some reading and we can do the following to get it to return multiple rows.我做了一些阅读,我们可以执行以下操作以使其返回多行。

myMockRows:=sqlmock.NewRows([]string{"col1","col2"})
.AddRow("col1val1", "col2val2")
.AddRow("col1val1", "col2val2")

mock.ExpectQuery("my_stored_procedure").WillReturnRows(myMockRows)

According to godoc .根据godoc Value slice return the same instance to perform subsequent actions.值切片返回相同的实例以执行后续操作。

Adding this blog post if you want an easier read about the topic如果您想更轻松地阅读该主题,请添加此博客文章

Do something like this:做这样的事情:

myMockRows:=sqlmock.NewRows([]string{"col1","col2"}).AddRow("col1val1", "col2val2")
myMockRows2:=sqlmock.NewRows([]string{"col3","col4"}).AddRow("col3val1", "col4val2")
mock.ExpectQuery("my_stored_procedure").WithArgs(arg1, arg2).WillReturnRows(myMockRows, myMockRows2)

Since WillReturnRows accepts multiple row objects and forms a slice, use it to construct the next Result Set.由于WillReturnRows接受多个行对象并形成一个切片,因此使用它来构造下一个 Result Set。

You have to get your test rows inside a struct and then execute loop over the struct like below code.您必须将测试行放入struct ,然后像下面的代码一样在struct执行loop

 type args struct {
        val string
 }

 tests := []struct {
            name    string
            s       *<YourDBstruct>
            args    args
            wantErr bool
        }{
            {
                name:    "Test with correct value1",
                s:       &YourDBstruct{db},
                args:    args{"Val1"}
                wantErr: true,
            },
            {
                name:    "Test with correct value2",
                s:       &YourDBstruct{db},
                args:    args{"Val2"}
                wantErr: true,
            },
            {
                name:    "Test with correct valueN",
                s:       &YourDBstruct{db},
                args:    args{"ValN"}
                wantErr: true,
            },
        }
        for _, tt := range tests {
            t.Run(tt.name, func(t *testing.T) {
                mock.ExpectExec("my_stored_procedure")
                if err := tt.s.YourStoreFuncName(); (err != nil) != tt.wantErr {
                    t.Errorf("YourStoreFuncName() error = %v, wantErr %v", err, tt.wantErr)
                }
            })

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

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