DATA-DOG / go-sqlmock

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

Runtime error using gorm #274

Closed austinjherman closed 2 years ago

austinjherman commented 2 years ago

Summary

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.

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.

austinjherman commented 2 years ago

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{}.