Skip to content

How should I test Body Binding in my application? #2526

Closed

Description

@joaoschoen

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?

Activity

  1. aldas commented on Oct 11, 2023

    @aldas
    Contributor

    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.

  2. locked and limited conversation to collaborators on Oct 11, 2023
  3. converted this issue into a discussion #2528 on Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions