The Context Factory provides a straightforward method for testing Gin Handlers by generating a test context.
This project has been developped by the Aloe team and is now open source.
The module provides:
- A simple method to create
gin.Context
- An easy way to test your
gin.Handlers
Emulate an API call by populating a gin.Context
thanks to the ContextOptions
structure:
type ContextOptions struct {
Method string
Path string
Body io.Reader
PathParams gin.Params
QueryParams map[string]string
Headers map[string]string
ContextVars map[string]interface{}
}
Use the factory method to create a gin.Context
and a httptest.ResponseRecorder
inside a unit test:
func TestGetDog(t *testing.T) {
options := contextfactory.ContextOptions{
Path: "/kid",
QueryParams: map[string]string{
"age": 3,
}
}
context, writer := contextfactory.BuildGinTestContext(testCase.Context)
GetDogs(context)
assert.Equal(t, http.StatusOK, writer.Code)
}
This section will be added soon.
Client is released under the MIT license. See LICENSE.txt.