Skip to content

Instantly share code, notes, and snippets.

@bastianccm
Created February 26, 2020 13:23
Show Gist options
  • Save bastianccm/d2e99fb9b83a0d1ca085d452b6f96582 to your computer and use it in GitHub Desktop.
Save bastianccm/d2e99fb9b83a0d1ca085d452b6f96582 to your computer and use it in GitHub Desktop.
package polls
import (
"context"
"log"
"net/http"
"testing"
"flamingo.me/flamingo/v3/framework/web"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jinzhu/gorm"
)
func newTestDb(t *testing.T) *gorm.DB {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
mock.ExpectBegin()
mock.ExpectExec(`SELECT * FROM "questions" WHERE "questions"."deleted_at" IS NULL ORDER BY id desc LIMIT 5`).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
gormDB, err := gorm.Open("sqlite", db)
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
return gormDB
}
func TestIndex(t *testing.T) {
db := newTestDb(t)
defer db.Close()
controller := new(controller)
controller.Inject(new(web.Responder), db)
response, ok := controller.index(context.Background(), web.CreateRequest(nil, nil)).(*web.RenderResponse)
if !ok {
log.Fatal("index() did not return a RenderResponse")
}
if response.Template != "index" {
log.Fatal("index() did not render the `index` template")
}
if response.DataResponse.Response.Status != http.StatusOK {
log.Fatal("index() did not return a HTTP 200 status")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment