go-sqlmock icon indicating copy to clipboard operation
go-sqlmock copied to clipboard

Ability to clone sqlmock

Open sanggonlee opened this issue 4 years ago • 1 comments
trafficstars

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.

sanggonlee avatar Nov 26 '20 20:11 sanggonlee

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

dolmen avatar Apr 22 '22 22:04 dolmen