Conditions | 6 |
Total Lines | 90 |
Code Lines | 68 |
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 TestClientLinkService_Create(t *testing.T) { |
||
14 | setEnv() |
||
15 | defer unsetEnv() |
||
16 | |||
17 | type args struct { |
||
18 | ctx context.Context |
||
19 | cd CreateClientLink |
||
20 | } |
||
21 | |||
22 | cases := []struct { |
||
23 | name string |
||
24 | args args |
||
25 | wantErr bool |
||
26 | err error |
||
27 | handler http.HandlerFunc |
||
28 | pre func() |
||
29 | }{ |
||
30 | { |
||
31 | "create new client link", |
||
32 | args{ |
||
33 | context.Background(), |
||
34 | CreateClientLink{}, |
||
35 | }, |
||
36 | false, |
||
37 | nil, |
||
38 | func(w http.ResponseWriter, r *http.Request) { |
||
39 | testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23") |
||
40 | testMethod(t, r, "POST") |
||
41 | if _, ok := r.Header[AuthHeader]; !ok { |
||
42 | w.WriteHeader(http.StatusUnauthorized) |
||
43 | } |
||
44 | |||
45 | _, _ = w.Write([]byte(testdata.CreateClientLinkResponse)) |
||
46 | }, |
||
47 | noPre, |
||
48 | }, |
||
49 | { |
||
50 | "create client link, an error is returned from the server", |
||
51 | args{ |
||
52 | context.Background(), |
||
53 | CreateClientLink{}, |
||
54 | }, |
||
55 | true, |
||
56 | fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."), |
||
57 | errorHandler, |
||
58 | noPre, |
||
59 | }, |
||
60 | { |
||
61 | "create client link, an error occurs when parsing json", |
||
62 | args{ |
||
63 | context.Background(), |
||
64 | CreateClientLink{}, |
||
65 | }, |
||
66 | true, |
||
67 | fmt.Errorf("invalid character 'h' looking for beginning of object key string"), |
||
68 | encodingHandler, |
||
69 | noPre, |
||
70 | }, |
||
71 | { |
||
72 | "create client link, invalid url when building request", |
||
73 | args{ |
||
74 | context.Background(), |
||
75 | CreateClientLink{}, |
||
76 | }, |
||
77 | true, |
||
78 | errBadBaseURL, |
||
79 | errorHandler, |
||
80 | crashSrv, |
||
81 | }, |
||
82 | } |
||
83 | |||
84 | for _, c := range cases { |
||
85 | setup() |
||
86 | defer teardown() |
||
87 | t.Run(c.name, func(t *testing.T) { |
||
88 | tMux.HandleFunc( |
||
89 | "/v2/client-links", |
||
90 | c.handler, |
||
91 | ) |
||
92 | c.pre() |
||
93 | |||
94 | res, cb, err := tClient.ClientLinks.Create(c.args.ctx, c.args.cd) |
||
95 | if c.wantErr { |
||
96 | assert.Error(t, err) |
||
97 | assert.EqualError(t, err, c.err.Error()) |
||
98 | } else { |
||
99 | assert.Nil(t, err) |
||
100 | assert.EqualValues(t, c.args.ctx, res.Request.Context()) |
||
101 | assert.IsType(t, &ClientLink{}, cb) |
||
102 | assert.IsType(t, &http.Response{}, res.Response) |
||
103 | } |
||
169 |