Skip to content

Received unexpected error when testing endpoints #2283

Answered by aldas
Geronaso asked this question in Q&A
Discussion options

You must be logged in to vote

If you are testing handlers directly ala handler.PostUser(c) then you need to have IF block to check if result was an error and if it is - check it against expected error. You could use e.ServeHTTP(rec, req) to test whole flow including middlewares (if they are set)

type CustomValidator struct {
	validator *validator.Validate
}

func (cv *CustomValidator) Validate(i interface{}) error {
	if err := cv.validator.Struct(i); err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return nil
}

type User struct {
	Name  string `json:"name" validate:"required"`
	Email string `json:"email" validate:"required,email"`
}

type handler struct {
}

func (h *handler) create(c e…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by Geronaso
Comment options

You must be logged in to vote
1 reply
@aldas
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants