Created
February 26, 2020 13:23
-
-
Save bastianccm/d2e99fb9b83a0d1ca085d452b6f96582 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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