| Conditions | 6 |
| Total Lines | 104 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package mollie |
||
| 178 | func TestDelayedRoutingService_List(t *testing.T) { |
||
| 179 | setEnv() |
||
| 180 | defer unsetEnv() |
||
| 181 | |||
| 182 | type args struct { |
||
| 183 | ctx context.Context |
||
| 184 | payment string |
||
| 185 | } |
||
| 186 | |||
| 187 | cases := []struct { |
||
| 188 | name string |
||
| 189 | args args |
||
| 190 | wantErr bool |
||
| 191 | err error |
||
| 192 | pre func() |
||
| 193 | handler http.HandlerFunc |
||
| 194 | }{ |
||
| 195 | { |
||
| 196 | "lists delayed routings successfully", |
||
| 197 | args{ |
||
| 198 | ctx: context.Background(), |
||
| 199 | payment: "tr_123456789", |
||
| 200 | }, |
||
| 201 | false, |
||
| 202 | nil, |
||
| 203 | noPre, |
||
| 204 | func(w http.ResponseWriter, r *http.Request) { |
||
| 205 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
| 206 | testMethod(t, r, "GET") |
||
| 207 | |||
| 208 | w.Header().Set("Content-Type", "application/json") |
||
| 209 | w.WriteHeader(http.StatusOK) |
||
| 210 | _, _ = w.Write([]byte(testdata.ListDelayedRoutingsExample)) |
||
| 211 | }, |
||
| 212 | }, |
||
| 213 | { |
||
| 214 | "lists delayed routings works as expected with access tokens", |
||
| 215 | args{ |
||
| 216 | ctx: context.Background(), |
||
| 217 | payment: "tr_123456789", |
||
| 218 | }, |
||
| 219 | false, |
||
| 220 | nil, |
||
| 221 | setAccessToken, |
||
| 222 | func(w http.ResponseWriter, r *http.Request) { |
||
| 223 | testHeader(t, r, AuthHeader, "Bearer access_token_test") |
||
| 224 | testMethod(t, r, "GET") |
||
| 225 | |||
| 226 | w.Header().Set("Content-Type", "application/json") |
||
| 227 | w.WriteHeader(http.StatusOK) |
||
| 228 | _, _ = w.Write([]byte(testdata.ListDelayedRoutingsExample)) |
||
| 229 | }, |
||
| 230 | }, |
||
| 231 | { |
||
| 232 | "list delayed routings fails with error in handler", |
||
| 233 | args{ |
||
| 234 | ctx: context.Background(), |
||
| 235 | payment: "tr_123456789", |
||
| 236 | }, |
||
| 237 | true, |
||
| 238 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"), |
||
| 239 | noPre, |
||
| 240 | errorHandler, |
||
| 241 | }, |
||
| 242 | { |
||
| 243 | "list delayed routings, an error occurs when parsing json", |
||
| 244 | args{ |
||
| 245 | ctx: context.Background(), |
||
| 246 | payment: "tr_123456789", |
||
| 247 | }, |
||
| 248 | true, |
||
| 249 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
| 250 | noPre, |
||
| 251 | encodingHandler, |
||
| 252 | }, |
||
| 253 | { |
||
| 254 | "list delayed routings, invalid url when building request", |
||
| 255 | args{ |
||
| 256 | ctx: context.Background(), |
||
| 257 | payment: "tr_123456789", |
||
| 258 | }, |
||
| 259 | true, |
||
| 260 | errBadBaseURL, |
||
| 261 | crashSrv, |
||
| 262 | errorHandler, |
||
| 263 | }, |
||
| 264 | } |
||
| 265 | |||
| 266 | for _, c := range cases { |
||
| 267 | setup() |
||
| 268 | defer teardown() |
||
| 269 | |||
| 270 | t.Run(c.name, func(t *testing.T) { |
||
| 271 | c.pre() |
||
| 272 | tMux.HandleFunc(fmt.Sprintf("/v2/payments/%s/routes", c.args.payment), c.handler) |
||
| 273 | |||
| 274 | res, m, err := tClient.DelayedRouting.List(c.args.ctx, c.args.payment) |
||
| 275 | if c.wantErr { |
||
| 276 | assert.NotNil(t, err) |
||
| 277 | assert.EqualError(t, err, c.err.Error()) |
||
| 278 | } else { |
||
| 279 | assert.Nil(t, err) |
||
| 280 | assert.IsType(t, &PaymentRoutesList{}, m) |
||
| 281 | assert.IsType(t, &http.Response{}, res.Response) |
||
| 282 | } |
||
| 286 |