Conditions | 10 |
Total Lines | 45 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like mollie.TestPaymentsList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package mollie |
||
13 | func TestPaymentsList(t *testing.T) { |
||
14 | setup() |
||
15 | defer teardown() |
||
16 | paymentsAPIEndpoint := "/v2/payments" |
||
17 | |||
18 | testMux.HandleFunc(paymentsAPIEndpoint, func(w http.ResponseWriter, r *http.Request) { |
||
19 | _, ok := r.Header[AuthHeader] |
||
20 | if !ok { |
||
21 | w.WriteHeader(http.StatusUnauthorized) |
||
22 | } |
||
23 | raw := testdata.ListPaymentsExample |
||
24 | fmt.Fprint(w, raw) |
||
25 | }) |
||
26 | |||
27 | err := testClient.WithAPIKey("dummy-key") |
||
28 | if err != nil { |
||
29 | t.Fatal(err) |
||
30 | } |
||
31 | |||
32 | var ctx context.Context |
||
33 | |||
34 | tests := []struct { |
||
35 | name string |
||
36 | want int |
||
37 | wantErr bool |
||
38 | err error |
||
39 | }{ |
||
40 | { |
||
41 | name: "test request is successful", |
||
42 | wantErr: false, |
||
43 | err: nil, |
||
44 | }, |
||
45 | } |
||
46 | |||
47 | for _, tt := range tests { |
||
48 | t.Run(tt.name, func(t *testing.T) { |
||
49 | _, err := testClient.Payments.List(ctx) |
||
50 | if tt.wantErr && err != nil { |
||
51 | if !reflect.DeepEqual(err, tt.err) { |
||
52 | t.Fatal(err) |
||
53 | } |
||
54 | } |
||
55 | |||
56 | if err != tt.err { |
||
57 | t.Errorf("wanted nil, got %+v", err) |
||
58 | } |
||
62 |