#4368·gofr

Mock SQL ExpectSelect/ExpectDialect/ExpectHealthCheck return pointers that go stale, and WillReturnHealthCheck(nil) panics

Author: NitinKumar004Created Sep 24, 2026Updated Sep 24, 2026

Describe the bug

The expectation helpers on the mock container's SQL mock return a pointer into a slice that later Expect* calls append to:

pkg/gofr/container/sql_mock.go:152-154, :176-178, :188-190

go
m.queryWithArgs = append(m.queryWithArgs, qr)

return &m.queryWithArgs[len(m.queryWithArgs)-1]
go
m.expectedHealthCheck = append(m.expectedHealthCheck, hc)

return &m.expectedHealthCheck[len(m.expectedHealthCheck)-1]
go
m.expectedDialect = append(m.expectedDialect, d)

return &m.expectedDialect[len(m.expectedDialect)-1]

When a later append has to grow the slice, the backing array is reallocated and the pointer handed out earlier now points into the old array. Anything set through it afterwards (ReturnsResponse, WillReturnString, WillReturnHealthCheck) is lost. Chaining straight away (ExpectDialect().WillReturnString("mysql")) works, but setting up several expectations first and filling them in afterwards does not.

Separately, WillReturnHealthCheck dereferences its argument without a nil check (sql_mock.go:181-183):

go
func (d *health) WillReturnHealthCheck(dh *datasource.Health) {
    *d = health(*dh)
}

so WillReturnHealthCheck(nil) (e.g. to mock a nil health result) panics.

To Reproduce

go
c, mocks := container.NewMockContainer(t)

d1 := mocks.SQL.ExpectDialect()
d2 := mocks.SQL.ExpectDialect()
d1.WillReturnString("mysql")
d2.WillReturnString("postgres")
c.SQL.Dialect(), c.SQL.Dialect() // "", "postgres"  (expected "mysql", "postgres")

h1 := mocks.SQL.ExpectHealthCheck()
h2 := mocks.SQL.ExpectHealthCheck()
h1.WillReturnHealthCheck(&datasource.Health{Status: "UP"})
h2.WillReturnHealthCheck(&datasource.Health{Status: "DOWN"})
c.SQL.HealthCheck().Status, c.SQL.HealthCheck().Status // "", "DOWN"  (expected "UP", "DOWN")

var a, b []string
s1 := mocks.SQL.ExpectSelect(ctx, &a, "select a")
s2 := mocks.SQL.ExpectSelect(ctx, &b, "select b")
s1.ReturnsResponse([]string{"A"})
s2.ReturnsResponse([]string{"B"})
c.SQL.Select(ctx, &a, "select a") // panics: reflect.Set: value of type *[]string is not assignable to type []string

mocks.SQL.ExpectHealthCheck().WillReturnHealthCheck(nil) // panics: nil pointer dereference

Expected behavior

  • Each Expect* call should return a handle that stays valid, e.g. store []*queryWithArgs / []*health / []*dialect and return the stored pointer.
  • WillReturnHealthCheck(nil) should either make HealthCheck() return nil or be rejected with a clear message, not panic.

Additional context

Found while working on #4360, which lists these as follow-ups.

Related: on development, unexpected calls to Select, HealthCheck and Dialect are only logged (sql_mock.go:55, :102, :118), so a test that makes an extra call still passes (missing calls are caught by finish). #4360 makes a Select mismatch fail the test through t. HealthCheck and Dialect would still only log after that PR, so they could use the same reporter.