Refer to the repo below for a simple reproduction. I'm trying to use go-sqlmock with gorm for testing database transactions. I want to write a test for the initial database migration, but I've hit a panic: runtime error: invalid memory address or nil pointer dereference and I've been having trouble figuring out why. Judging by the error stack, I think it's this statement that does it: db.AutoMigrate(&user{}). I'm not sure why, as db has allegedly started up successfully by this point and user is defined and instantiated as an argument to db.AutoMigrate.
Figured it out. It was a config option: &gorm.Config{ PrepareStmt: true }. While this works in production, it does not work with sqlmock. Fixed it by changing it to: &gorm.Config{}.
Summary
Refer to the repo below for a simple reproduction. I'm trying to use
go-sqlmock
withgorm
for testing database transactions. I want to write a test for the initial database migration, but I've hit apanic: runtime error: invalid memory address or nil pointer dereference
and I've been having trouble figuring out why. Judging by the error stack, I think it's this statement that does it:db.AutoMigrate(&user{})
. I'm not sure why, asdb
has allegedly started up successfully by this point anduser
is defined and instantiated as an argument todb.AutoMigrate
.Simple reproduction
https://github.com/austinjherman/go-sqlmock-runtime-error
Instructions
Fork or clone the repo and from the project root run
go test .
Behold the error.