| Conditions | 22 |
| Total Lines | 51 |
| Lines | 0 |
| Ratio | 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:
Complex classes like describe_get() 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 | # pylint: disable=unused-variable |
||
| 7 | def describe_get(): |
||
| 8 | |||
| 9 | def when_default_text(client): |
||
| 10 | response = client.get("/templates/iw") |
||
| 11 | |||
| 12 | assert 200 == response.status_code |
||
| 13 | assert dict( |
||
| 14 | name="Insanity Wolf", |
||
| 15 | description="http://knowyourmeme.com/memes/insanity-wolf", |
||
| 16 | aliases=['insanity', 'insanity-wolf', 'iw'], |
||
| 17 | styles=[], |
||
| 18 | example="http://localhost/iw/does-testing/in-production", |
||
| 19 | ) == load(response) |
||
| 20 | |||
| 21 | def when_no_default_text(client): |
||
| 22 | response = client.get("/templates/keanu") |
||
| 23 | |||
| 24 | assert 200 == response.status_code |
||
| 25 | assert "http://localhost/keanu/your-text/goes-here" == \ |
||
| 26 | load(response)['example'] |
||
| 27 | |||
| 28 | def when_alternate_sytles_available(client): |
||
| 29 | response = client.get("/templates/sad-biden") |
||
| 30 | |||
| 31 | assert 200 == response.status_code |
||
| 32 | assert ['down', 'scowl', 'window'] == load(response)['styles'] |
||
| 33 | |||
| 34 | def when_dashes_in_key(client): |
||
| 35 | response = client.get("/templates/awkward-awesome") |
||
| 36 | |||
| 37 | assert 200 == response.status_code |
||
| 38 | |||
| 39 | def it_returns_list_when_no_key(client): |
||
| 40 | response = client.get("/templates/") |
||
| 41 | |||
| 42 | assert 200 == response.status_code |
||
| 43 | data = load(response) |
||
| 44 | assert "http://localhost/templates/iw" == data['Insanity Wolf'] |
||
| 45 | assert len(data) >= 20 # there should be many memes |
||
| 46 | |||
| 47 | def it_redirects_when_text_is_provided(client): |
||
| 48 | response = client.get("/templates/iw/top/bottom") |
||
| 49 | |||
| 50 | assert 302 == response.status_code |
||
| 51 | assert '<a href="/iw/top/bottom">' in load(response, as_json=False) |
||
| 52 | |||
| 53 | def it_redirects_when_key_is_an_alias(client): |
||
| 54 | response = client.get("/templates/insanity-wolf") |
||
| 55 | |||
| 56 | assert 302 == response.status_code |
||
| 57 | assert '<a href="/templates/iw">' in load(response, as_json=False) |
||
| 58 | |||
| 69 |