@@ -12,7 +12,12 @@ func TestMyController(t *testing.T) {
12
12
ctx := fuego.NewMockContext [MyRequestType](MyRequestType{
13
13
Name: " John" ,
14
14
Age: 30 ,
15
- }
15
+ })
16
+
17
+ // Add query parameters with OpenAPI validation
18
+ ctx.WithQueryParamInt (" page" , 1 ,
19
+ fuego.ParamDescription (" Page number" ),
20
+ fuego.ParamDefault (1 ))
16
21
17
22
// Call your controller
18
23
response , err := MyController (ctx)
@@ -60,7 +65,7 @@ func TestSearchUsersController(t *testing.T) {
60
65
tests := []struct {
61
66
name string
62
67
body UserSearchRequest
63
- queryParams url. Values
68
+ setupContext func (*fuego. MockContext [UserSearchRequest])
64
69
expectedError string
65
70
expected UserSearchResponse
66
71
}{
@@ -71,31 +76,30 @@ func TestSearchUsersController(t *testing.T) {
71
76
MaxAge: 35 ,
72
77
NameQuery: " John" ,
73
78
},
74
- queryParams: url.Values {
75
- " page" : {" 1" },
79
+ setupContext: func (ctx *fuego.MockContext [UserSearchRequest]) {
80
+ // Add query parameters with OpenAPI validation
81
+ ctx.WithQueryParamInt (" page" , 1 ,
82
+ fuego.ParamDescription (" Page number" ),
83
+ fuego.ParamDefault (1 ))
84
+ ctx.WithQueryParamInt (" perPage" , 20 ,
85
+ fuego.ParamDescription (" Items per page" ),
86
+ fuego.ParamDefault (20 ))
76
87
},
77
88
expected: UserSearchResponse{
78
89
// ... expected response
79
90
},
80
91
},
81
- {
82
- name: " invalid age range" ,
83
- body: UserSearchRequest{
84
- MinAge: 40 ,
85
- MaxAge: 20 ,
86
- NameQuery: " John" ,
87
- },
88
- expectedError: " minAge cannot be greater than maxAge" ,
89
- },
90
92
}
91
93
92
94
for _ , tt := range tests {
93
95
t.Run (tt.name , func (t *testing.T ) {
94
96
// Create mock context with the test body
95
97
ctx := fuego.NewMockContext [UserSearchRequest](tt.body )
96
98
97
- // Set query parameters directly
98
- ctx.UrlValues = tt.queryParams
99
+ // Set up context with query parameters
100
+ if tt.setupContext != nil {
101
+ tt.setupContext (ctx)
102
+ }
99
103
100
104
// Call the controller
101
105
response , err := SearchUsersController (ctx)
@@ -114,15 +118,29 @@ func TestSearchUsersController(t *testing.T) {
114
118
}
115
119
```
116
120
117
- ## Available Fields
121
+ ## Available Fields and Methods
122
+
123
+ The ` MockContext ` type provides the following:
118
124
119
- The ` MockContext ` type provides the following public fields for testing :
125
+ Public Fields :
120
126
121
127
- ` RequestBody ` - The request body of type B
122
128
- ` Headers ` - HTTP headers
123
129
- ` PathParams ` - URL path parameters
124
130
- ` Cookies ` - HTTP cookies
125
- - ` UrlValues ` - Query parameters
131
+
132
+ Helper Methods for Query Parameters:
133
+
134
+ - ` WithQueryParam(name, value string, options ...func(*OpenAPIParam)) ` - Add a string query parameter
135
+ - ` WithQueryParamInt(name string, value int, options ...func(*OpenAPIParam)) ` - Add an integer query parameter
136
+ - ` WithQueryParamBool(name string, value bool, options ...func(*OpenAPIParam)) ` - Add a boolean query parameter
137
+
138
+ Each helper method accepts OpenAPI parameter options like:
139
+
140
+ - ` ParamDescription(description string) ` - Add parameter description
141
+ - ` ParamDefault(value any) ` - Set default value
142
+ - ` ParamRequired() ` - Mark parameter as required
143
+ - ` ParamExample(name string, value any) ` - Add example value
126
144
127
145
## Best Practices
128
146
0 commit comments