Here’s a cute way to test Go code. From How To Implement Domain-Driven Design (DDD) in Golang.
1package memory2 3import (4 "testing"5 6 "ddd/aggregate"7 "ddd/domain/customer"8 9 "github.com/google/uuid"10)11 12func TestMemory_GetCustomer(t *testing.T) {13 type testCase struct {14 expectedErr error15 name string16 id uuid.UUID17 }18 19 // Create a fake customer to add to repository20 cust, err := aggregate.NewCustomer("Percy")21 if err != nil {22 t.Fatal(err)23 }24 id := cust.GetID()25 // Create the repo to use, and add some test Data to it for testing26 // Skip Factory for this27 repo := MemoryRepository{28 customers: map[uuid.UUID]aggregate.Customer{29 id: cust,30 },31 }32 33 testCases := []testCase{34 {35 name: "No Customer By ID",36 id: uuid.MustParse("f47ac10b-58cc-0372-8567-0e02b2c3d479"),37 expectedErr: customer.ErrCustomerNotFound,38 }, {39 name: "Customer By ID",40 id: id,41 expectedErr: nil,42 },43 }44 45 for _, tc := range testCases {46 t.Run(tc.name, func(t *testing.T) {47 _, err := repo.Get(tc.id)48 if err != tc.expectedErr {49 t.Errorf("Expected error %v, got %v", tc.expectedErr, err)50 }51 })52 }53}54 55func TestMemory_AddCustomer(t *testing.T) {56 type testCase struct {57 name string58 cust string59 expectedErr error60 }61 62 testCases := []testCase{63 {64 name: "Add Customer",65 cust: "Percy",66 expectedErr: nil,67 },68 }69 70 for _, tc := range testCases {71 t.Run(tc.name, func(t *testing.T) {72 repo := MemoryRepository{73 customers: map[uuid.UUID]aggregate.Customer{},74 }75 76 cust, err := aggregate.NewCustomer(tc.cust)77 if err != nil {78 t.Fatal(err)79 }80 81 err = repo.Add(cust)82 if err != tc.expectedErr {83 t.Errorf("Expected error %v, got %v", tc.expectedErr, err)84 }85 86 found, err := repo.Get(cust.GetID())87 if err != nil {88 t.Fatal(err)89 }90 if found.GetID() != cust.GetID() {91 t.Errorf("Expected %v, got %v", cust.GetID(), found.GetID())92 }93 })94 }95}