| Conditions | 5 |
| Total Lines | 86 |
| Code Lines | 65 |
| 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 |
||
| 13 | func TestWebhookEventsService_Get(t *testing.T) { |
||
| 14 | setEnv() |
||
| 15 | defer unsetEnv() |
||
| 16 | |||
| 17 | type args struct { |
||
| 18 | ctx context.Context |
||
| 19 | webhook string |
||
| 20 | } |
||
| 21 | |||
| 22 | cases := []struct { |
||
| 23 | name string |
||
| 24 | args args |
||
| 25 | wantErr bool |
||
| 26 | err error |
||
| 27 | pre func() |
||
| 28 | handler http.HandlerFunc |
||
| 29 | }{ |
||
| 30 | { |
||
| 31 | "get webhook events works as expected with access token", |
||
| 32 | args{ |
||
| 33 | context.Background(), |
||
| 34 | "event_GvJ8WHrp5isUdRub9CJyH", |
||
| 35 | }, |
||
| 36 | false, |
||
| 37 | nil, |
||
| 38 | setAccessToken, |
||
| 39 | func(w http.ResponseWriter, r *http.Request) { |
||
| 40 | testHeader(t, r, AuthHeader, "Bearer access_token_test") |
||
| 41 | testMethod(t, r, "GET") |
||
| 42 | |||
| 43 | w.Header().Set("Content-Type", "application/json") |
||
| 44 | w.WriteHeader(http.StatusOK) |
||
| 45 | _, _ = w.Write([]byte(testdata.GetWebhookEventExample)) |
||
| 46 | }, |
||
| 47 | }, |
||
| 48 | { |
||
| 49 | "get webhook events returns an error when server responds with error", |
||
| 50 | args{ |
||
| 51 | context.Background(), |
||
| 52 | "event_GvJ8WHrp5isUdRub9CJyH", |
||
| 53 | }, |
||
| 54 | true, |
||
| 55 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request"), |
||
| 56 | setAccessToken, |
||
| 57 | errorHandler, |
||
| 58 | }, |
||
| 59 | { |
||
| 60 | "get webhook events, an error returned when parsing JSON", |
||
| 61 | args{ |
||
| 62 | context.Background(), |
||
| 63 | "event_GvJ8WHrp5isUdRub9CJyH", |
||
| 64 | }, |
||
| 65 | true, |
||
| 66 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
| 67 | setAccessToken, |
||
| 68 | encodingHandler, |
||
| 69 | }, |
||
| 70 | { |
||
| 71 | "get webhook events fails when building request with invalid url", |
||
| 72 | args{ |
||
| 73 | context.Background(), |
||
| 74 | "event_GvJ8WHrp5isUdRub9CJyH", |
||
| 75 | }, |
||
| 76 | true, |
||
| 77 | errBadBaseURL, |
||
| 78 | crashSrv, |
||
| 79 | errorHandler, |
||
| 80 | }, |
||
| 81 | } |
||
| 82 | |||
| 83 | for _, c := range cases { |
||
| 84 | setup() |
||
| 85 | defer teardown() |
||
| 86 | |||
| 87 | t.Run(c.name, func(t *testing.T) { |
||
| 88 | c.pre() |
||
| 89 | tMux.HandleFunc("/v2/events/"+c.args.webhook, c.handler) |
||
| 90 | |||
| 91 | res, m, err := tClient.WebhookEvents.Get(c.args.ctx, c.args.webhook) |
||
| 92 | if c.wantErr { |
||
| 93 | assert.NotNil(t, err) |
||
| 94 | assert.EqualError(t, err, c.err.Error()) |
||
| 95 | } else { |
||
| 96 | assert.Nil(t, err) |
||
| 97 | assert.IsType(t, &WebhookEvent{}, m) |
||
| 98 | assert.IsType(t, &http.Response{}, res.Response) |
||
| 99 | } |
||
| 103 |