Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing page not describing tests clearly for more than one route #202

Open
LucasDSL opened this issue Sep 26, 2022 · 0 comments
Open

Testing page not describing tests clearly for more than one route #202

LucasDSL opened this issue Sep 26, 2022 · 0 comments

Comments

@LucasDSL
Copy link

LucasDSL commented Sep 26, 2022

Situation:

The Testing is not clear enough when we need to test more than one route.

For instance, if we have multiple routes in our project, such as this

func setupRouter() *gin.Engine {
	router := gin.Default()
	router.GET("/albums", getAlbums)
	router.POST("/album", addAlbum)
	router.GET("/albums/:id", getAlbumById)
	return router
}

based on the documentation, how can we test all this routes and also avoid calling setupRouter for every test function? as I did in the project above

func TestGetAlbumsRoute(t *testing.T) {
	router := setupRouter()
	w := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/albums", nil)
	router.ServeHTTP(w, req)

	assert.Equal(t, 200, w.Code)
	assert.Equal(t, "[{\"id\":\"1\",\"title\":\"Blue Train\",\"artist\":\"John Coltrane\",\"price\":56.99},{\"id\":\"2\",\"title\":\"Jeru\",\"artist\":\"Gerry Mulligan\",\"price\":17.99},{\"id\":\"3\",\"title\":\"Sarah Vaughan and Clifford Brown\",\"artist\":\"Sarah Vaughan\",\"price\":39.99}]", w.Body.String())
}

func TestGetAlbumById(t *testing.T) {
	router := setupRouter()
	w := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/albums/2", nil)
	router.ServeHTTP(w, req)

	assert.Equal(t, 200, w.Code)
	assert.JSONEq(t, 
		"{\"id\":\"2\",\"title\":\"Jeru\",\"artist\":\"Gerry Mulligan\",\"price\":17.99}", 
		w.Body.String())
}

The documentation could have just the one example with multiple routes so this scenario would be more wide explained and help more code with basic apis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant