logic_test.tpl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Code scaffolded by goctl. Safe to edit.
  2. // goctl {{.version}}
  3. package {{.pkgName}}
  4. import (
  5. "context"
  6. "testing"
  7. {{.imports}}
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func Test{{.logic}}_{{.function}}(t *testing.T) {
  12. c := config.Config{}
  13. mockSvcCtx := svc.NewServiceContext(c)
  14. // init mock service context here
  15. tests := []struct {
  16. name string
  17. ctx context.Context
  18. setupMocks func()
  19. {{if .hasRequest}}req *{{.requestType}}{{end}}
  20. wantErr bool
  21. checkResp func{{if .hasResponse}}{{.responseType}}{{else}}(err error){{end}}
  22. }{
  23. {
  24. name: "response error",
  25. ctx: context.Background(),
  26. setupMocks: func() {
  27. // mock data for this test case
  28. },
  29. {{if .hasRequest}}req: &{{.requestType}}{
  30. // TODO: init your request here
  31. },{{end}}
  32. wantErr: true,
  33. checkResp: func{{if .hasResponse}}{{.responseType}}{{else}}(err error){{end}} {
  34. // TODO: Add your check logic here
  35. },
  36. },
  37. {
  38. name: "successful",
  39. ctx: context.Background(),
  40. setupMocks: func() {
  41. // Mock data for this test case
  42. },
  43. {{if .hasRequest}}req: &{{.requestType}}{
  44. // TODO: init your request here
  45. },{{end}}
  46. wantErr: false,
  47. checkResp: func{{if .hasResponse}}{{.responseType}}{{else}}(err error){{end}} {
  48. // TODO: Add your check logic here
  49. },
  50. },
  51. }
  52. for _, tt := range tests {
  53. t.Run(tt.name, func(t *testing.T) {
  54. tt.setupMocks()
  55. l := New{{.logic}}(tt.ctx, mockSvcCtx)
  56. {{if .hasResponse}}resp, {{end}}err := l.{{.function}}({{if .hasRequest}}tt.req{{end}})
  57. if tt.wantErr {
  58. assert.Error(t, err)
  59. } else {
  60. require.NoError(t, err)
  61. {{if .hasResponse}}assert.NotNil(t, resp){{end}}
  62. }
  63. tt.checkResp({{if .hasResponse}}resp, {{end}}err)
  64. })
  65. }
  66. }