DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.02k stars 406 forks source link

Ability to clone sqlmock #247

Closed sanggonlee closed 2 months ago

sanggonlee commented 3 years ago

Hello, I think it would be great to be able to reuse mock instead of creating a new instance for every test case. Something like:

func TestSomething(t *testing.T) {
    db, mock, err := sqlmock.New()
    if err != nil {
        t.Fatal(err)
    }

    cases := []struct {
        description string
        input       string
        expected    string
    }{
        {
            description: "abc",
            input:       "a",
            expected:    "b",
        },
    }

    for _, c := range cases {
        t.Run(c.description, func(t *testing.T) {
            mock = mock.Clone()

            //...
        })
    }
}

Will be happy to contribute if you think this is a good idea.

dolmen commented 2 years ago

Why not just put the sqlmock.New() in the subtest?