Gorm: Unit Testing

Created on 3 Nov 2015  路  4Comments  路  Source: go-gorm/gorm

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?

question

Most helpful comment

@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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Quentin-M picture Quentin-M  路  3Comments

bramp picture bramp  路  3Comments

Ganitzsh picture Ganitzsh  路  3Comments

sredxny picture sredxny  路  3Comments

youtwo123 picture youtwo123  路  3Comments