Open rafa-acioly opened 4 years ago
@rafa-acioly From what I can tell the loop is likely never entered because r.database
is never overwritten with the mock suite.database
.
For example I would expect to see something like:
suite.repository.database = suite.database
@rmulley i overwrite the database when i create a New Repository
, here's the code from NewRepository
type Repository struct {
database *sql.DB
}
func NewRepository(db *sql.DB) Repository {
return Repository{database: db}
}
func (r Repository) Index() ([]Mystruct, error) {
rows, _ := r.database.Query("SELECT * FROM table WHERE start_at >= NOW()")
for rows.Next() {} // never enter the loop because the result is empty
}
So when i Setup
the test:
func (suite *RepositoryTestSuit) SetupSuite() {
db, mock, _ := sqlmock.New()
suite.mock = mock
suite.database = db
suite.repository = NewRepository(db) // <- passes the database to the repository
}
It is possible to filter a row within the mocked rows? When i try to perform a query the result is always empty.
The repository: