labstack / echo

High performance, minimalist Go web framework

Home Page:https://echo.labstack.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How should I test Body Binding in my application?

joaoschoen opened this issue · comments

I'm writing an API using echo and adding tests to it as I go, and I've found a problem that I don't know how to solve

In every single request that requires the binding of a body to a struct I have to add a test case for if the binding fails

var loginInfo model.UnsafeUser
  if err := echo.Bind(&loginInfo); err != nil {
  return echo.JSON(http.StatusBadRequest, "Bad username and password combination")
}

Something like this bit of code above, do you guys have any solution for it other then just adding the same test to every single test file?

I do not think there is magic bullet here. Just copy/paste testcase for each handler with invalid body. ala {. Probably designing something generic/etc here is not worth the complexity/time/effort. My experience is that I spend multiple orders of magnitude more time in application "business" layer than meddling with handlers and their tests.