✨ Add HandlerWithError
This commit is contained in:
parent
371388838e
commit
447f3b38ef
4 changed files with 186 additions and 0 deletions
52
handler_test.go
Normal file
52
handler_test.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
suite.Run(t, new(HandlerTestSuite))
|
||||
}
|
||||
|
||||
type HandlerTestSuite struct {
|
||||
suite.Suite
|
||||
w *httptest.ResponseRecorder
|
||||
ctx *gin.Context
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) SetupTest() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
s.w = httptest.NewRecorder()
|
||||
s.ctx, _ = gin.CreateTestContext(s.w)
|
||||
s.ctx.Request = new(http.Request)
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TearDownTest() {
|
||||
s.w = nil
|
||||
s.ctx = nil
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TestHandler() {
|
||||
err := errors.New("Goodbye")
|
||||
HandlerWithErrorWrapper(func(c *gin.Context) error {
|
||||
return err
|
||||
})(s.ctx)
|
||||
|
||||
found := s.ctx.Errors.Last()
|
||||
s.Assert().True(errors.Is(found, err))
|
||||
}
|
||||
|
||||
func (s *HandlerTestSuite) TestHandlerNil() {
|
||||
HandlerWithErrorWrapper(func(c *gin.Context) error {
|
||||
return nil
|
||||
})(s.ctx)
|
||||
|
||||
found := s.ctx.Errors.Last()
|
||||
s.Assert().Nil(found)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue