Last active
April 26, 2019 15:28
Revisions
-
wtask renamed this gist
Apr 26, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
wtask revised this gist
Apr 26, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -41,7 +41,7 @@ func ServiceMethodNext(s CoolService, testDB *gorm.DB) test { // setupDB - creates independent db connection to track changes made by CoolService, // returns *gorm.DB only as example func setupDB() *gorm.DB { // read config, find DSN ... db, err := gorm.Open(...) if err != nil { -
wtask revised this gist
Apr 26, 2019 . 1 changed file with 7 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,15 +10,15 @@ import ( type test func(t *testing.T) // CoolServiceSuite - group of CoolService tests func CoolServiceSuite(s CoolService, testDB *gorm.DB) []test { return []test{ ServiceMethodFirst(s, testDB), ServiceMethodNext(s, testDB), } } // ServiceMethodFirst - make test for CoolService.MethodFirst() func ServiceMethodFirst(s CoolService, testDB *gorm.DB) test { return func(t *testing.T) { t.Log("TEST service.CoolService.MethodFirst()") value, err := s.MethodFirst() @@ -28,11 +28,12 @@ func ServiceMethodFirst(s CoolService) test { if value != "first is cool" { t.Errorf("Unexpected result: %s", value) } // check DB changes with testDB } } // ServiceMethodNext - make test for CoolService.MethodNext() func ServiceMethodNext(s CoolService, testDB *gorm.DB) test { return func(t *testing.T) { // test something } @@ -62,7 +63,7 @@ func TestCoolService(t *testing.T) { }() cool, err := NewCoolService() for i, test := range CoolServiceSuite(cool, testDB) { t.Run(fmt.Sprintf("test #%d", i), test) } } -
wtask revised this gist
Apr 26, 2019 . No changes.There are no files selected for viewing
-
wtask created this gist
Apr 26, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ package service import ( "fmt" "testing" "github.com/jinzhu/gorm" ".../service/model" ) type test func(t *testing.T) // CoolServiceSuite - group of CoolService tests func CoolServiceSuite(s CoolService) []test { return []test{ ServiceMethodFirst(s), ServiceMethodNext(s), } } // ServiceMethodFirst - make test for CoolService.MethodFirst() func ServiceMethodFirst(s CoolService) test { return func(t *testing.T) { t.Log("TEST service.CoolService.MethodFirst()") value, err := s.MethodFirst() if err != nil { t.Errorf("Unexpected error: %v", err) } if value != "first is cool" { t.Errorf("Unexpected result: %s", value) } } } // ServiceMethodNext - make test for CoolService.MethodNext() func ServiceMethodNext(s CoolService) test { return func(t *testing.T) { // test something } } // setupDB - creates independent db connection to track changes made by CoolService, // returns *gorm.DB only as example func setupServiceDB() *gorm.DB { // read config, find DSN ... db, err := gorm.Open(...) if err != nil { panic(err) } // prepare something or make migrations ... return db } func TestCoolService(t *testing.T) { testDB := setupDB() // additional setup ... defer func() { // tear down testDB.Close() // ... }() cool, err := NewCoolService() for i, test := range CoolServiceSuite(cool) { t.Run(fmt.Sprintf("test #%d", i), test) } }