Skip to content

Instantly share code, notes, and snippets.

@joshuabezaleel
Created January 8, 2020 03:14
Show Gist options
  • Save joshuabezaleel/ba056fa2c67e2fb11e7c0e320770b41b to your computer and use it in GitHub Desktop.
Save joshuabezaleel/ba056fa2c67e2fb11e7c0e320770b41b to your computer and use it in GitHub Desktop.
func TestGet(t *testing.T) {
mockDB, mock, err := sqlmock.New()
require.Nil(t, err)
defer mockDB.Close()
db := sqlx.NewDb(mockDB, "sqlmock")
bookRepository := NewBookRepository(db)
rows := sqlmock.NewRows([]string{"id, title, author"}).
AddRow("testID", "testTitle", "testAuthor")
mock.ExpectQuery("SELECT * FROM books WHERE id=?").
WithArgs("testID").
WillReturnRows(rows)
newBook, err := bookRepository.Get("testID")
require.Nil(t, err)
require.Equal(t, newBook.ID, "testID")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment