Skip to content

Instantly share code, notes, and snippets.

@wtask
Last active April 26, 2019 15:28

Revisions

  1. wtask renamed this gist Apr 26, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. wtask revised this gist Apr 26, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package_test.go
    Original 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 setupServiceDB() *gorm.DB {
    func setupDB() *gorm.DB {
    // read config, find DSN ...
    db, err := gorm.Open(...)
    if err != nil {
  3. wtask revised this gist Apr 26, 2019. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions package_test.go
    Original 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) []test {
    func CoolServiceSuite(s CoolService, testDB *gorm.DB) []test {
    return []test{
    ServiceMethodFirst(s),
    ServiceMethodNext(s),
    ServiceMethodFirst(s, testDB),
    ServiceMethodNext(s, testDB),
    }
    }

    // ServiceMethodFirst - make test for CoolService.MethodFirst()
    func ServiceMethodFirst(s CoolService) test {
    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) test {
    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) {
    for i, test := range CoolServiceSuite(cool, testDB) {
    t.Run(fmt.Sprintf("test #%d", i), test)
    }
    }
  4. wtask revised this gist Apr 26, 2019. No changes.
  5. wtask created this gist Apr 26, 2019.
    68 changes: 68 additions & 0 deletions package_test.go
    Original 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)
    }
    }