My Go project contains multiple application-specific database functions, and I'm looking to test my application as effectively as possible. I have read on many blogs about the power of interfaces, and how they are able to increase testability:
package db
type MyDataStore interface {
QueryRecentUsers(interval int) ([]*model.User, error)
}
type myDataStore struct {
*gorm.DB
}
func (m *myDataStore) QueryRecentUsers(interval int) ([]*model.User, error) {
...
}
In this example, the QueryRecentUsers
function is capable of being stubbed by any consuming module. While this is of great benefit, it does not allow for easier testing within the db
package. QueryRecentUsers
may contain logic of its own, but without a true database connection the code cannot be tested. Short of mocking, which appears to be somewhat controversial, what is the best practice for testing application-specific database code? Are there any other considerations when designing testable go code?
Aucun commentaire:
Enregistrer un commentaire