Is there a support for writing unit tests for code using gorm? Some sort of easy way to mock SQL queries done by the library?
I think the most common way to accomplish what you're looking for is my simply using the sqlite3 database driver.
import (
_ "github.com/mattn/go-sqlite3"
)
Many other frameworks (like Django in the Python world) actually recommend using Sqlite3 as your database engine when running tests. Hope that helps!
Or checkout this one. https://github.com/erikstmartin/go-testdb
But I do suggest you test with the same database you are using in production.
@jinzhu thanks for pointing out go-testdb. curious to know if there is dialect support for this in gorm. I really do think having this feature will make things a lot easier.
@RichardKnop @mark-adams @strin @pedromorgan
I use sqlmock to mock-test GORM queries. Example is here, search e.g. testUserSelectAll.
First I tried go-testdb and custom gorm.DB wrapper to interface, but current solution with sqlmock is the simplest.
Most helpful comment
@jinzhu thanks for pointing out
go-testdb. curious to know if there is dialect support for this ingorm. I really do think having this feature will make things a lot easier.